0

Can someone please tell me whats wrong with this code

# This is a guess the number game. import random secretNumber = random.randint(1, 20) print('I am thinking of a number between 1 an 20.') # Ask the player to guess 6 times. for guessTaken in range(1, 7): print('Take a guess.') guess = int(input()) if guess < secretNumber: print('Your guess is too low.') elif guess > secretNumber: print('Your guess is too high') else: break # This condition is the correct guess! if guess == secretNumber: print('Good job! You guessed my number in' + str(guessTaken) + 'guesses!') else: print('Nope. The number I was thinking of was ' + str(secretNumber)) When i guessed the good number it prints: Good job! You guessed my number in4guesses! I don't what that it prints ''in4guesses!'' without spaces between. I'm a beginner with coding, so i hope someone can explain me what i'm doing wrong.

13th Jan 2023, 12:01 PM
Giorgio
Giorgio - avatar
3 Answers
+ 2
The reason why it prints **inNguesses** is because you did string concatenation wrong. There are 2 ways in which you can fix it: 1. Put spaces: "... number in<space>" + str(secretNumber) + "<space> guesses!") So it will be: "...number in " + str(secretNumber) + " guesses!") 2. Separate with commas. If you use commas, they will automatically add the space between strings/variable(and whatever you put in). If you use commas, you don't have to use str() function, as they will make spaces automatically and add objects together. even if it's an integer and a string. Therefore it will either look like: '... number in', str(guessTaken), 'guesses!') OR '...number in', guessTaken, 'guesses!' But the game is good, keep going! I did a similar one the other day, but that's just for one time play on sololearn.
13th Jan 2023, 1:15 PM
Lamron
Lamron - avatar
+ 1
Hi Lamron, I knew the solution would be that simple, but i didn't see it... Now i get it! Thanks for taking the time to explain it to me in different ways.
13th Jan 2023, 1:52 PM
Giorgio
Giorgio - avatar
+ 1
Giorgio not a problem, it happens
13th Jan 2023, 1:53 PM
Lamron
Lamron - avatar