Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I keep geting an invalid syntax error in the
forguessesTaken in range(1, 7): line. Python keeps saying the : is invalid syntax. This is driving me crazy. Can anyone help?
# this is a guess the number game.
import random
secretnumber = random.randint(1,20)
print('I am thinking of a number between 1 and 20.')
# ask the player to guess 6 times.
forguessesTaken 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 corret guess!
if guess == secret number:
print('good job! You guessed my number in ' + str(guessestaken) + 'guesses!')
else:
print('nope. the number i was thinking of was ' + str(secretnumber))
There is no keyword in python called forguessesTaken. Maybe you forget to put space between for and guessesTaken. Put a space between them:
for guessesTaken in range(1,7):
// remaining code...
Use this code instead:
for guessesTaken in range(1, 8):
if guessesTaken == 8:
print('Nope. The number I was thinking of was', secretnumber)
break
guess = int(input('Take a guess: '))
if guess < secretnumber:
print('Your guess is too low.')
elif guess > secretnumber:
print('Your guess is too high.')
else:
print('Good job! You guessed my number in', guessesTaken, 'guesses!')
break
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
import random
secret_number = random.randint(1, 20)
print("I'm thinking of a number between 1 and 20")
for guesses_taken in range(1, 7):
guess = int(input("Take a guess"))
if guess < secret_number:
print("This number is too low")
elif guess > secret_number:
print("This number is too high")
else:
break
if guesses_taken == 6:
print(f"Nope, the number I was thinking about was {secret_number}")
#this line won't display even when the number is guessed correctly
if guess == secret_number and guesses_taken < 6:
print(f"Good job! You guessed my number in {guesses_taken} guesses!")
The original code had an indentation error, which I fixed by moving the last four lines of code to the loop teritory. Sadly, the last line still won't display even though the number was guessed correctly.
That's because you have indentation wrong. Everything below break should be unindented, so that it's not part of the for loop:
import random
secret_number = random.randint(1, 20)
print("I'm thinking of a number between 1 and 20")
for guesses_taken in range(1, 7):
guess = int(input("Take a guess"))
if guess < secret_number:
print("This number is too low")
elif guess > secret_number:
print("This number is too high")
else:
break
if guesses_taken == 6:
print(f"Nope, the number I was thinking about was {secret_number}")
if guess == secret_number and guesses_taken < 6:
print(f"Good job! You guessed my number in {guesses_taken} guesses!")
However, I would like to add that even this version of the code has a bug: if a user guessed correctly on his last attempt, the output would still be Nope, the number I was thinking about was {secret_number}
First of all you don't really need the
if guesses_taken == 6:
The problem you're having is probably the break statement is on the same indentation level as the
if guess == secret_number and guesses_taken < 6:
Here is your code that should work fine.
import random
secret_number = random.randint(1, 20)
print("I'm thinking of a number between 1 and 20")
for guesses_taken in range(1, 7):
guess = int(input("Take a guess"))
if guess == secret_number:
print(f"Good job! You guessed my number in {guesses_taken} guesses!")
break
elif guess < secret_number:
print("This number is too low")
else:
print("This number is too high")
else:
print(f"Nope, the number I was thinking about was {secret_number}")
You don't really need the < 6 check because this will happen when you exceed your for loops range which is from 1 till 7
TL;DR:
Remove lines 12-13
Lines 8 through 13:
if guess < secret_number:
print("This number is too low")
elif guess > secret_number:
print("This number is too high")
else:
break
Think about this logically:
If the guess is less than the chosen number, inform the user that their guess is too low
If the guess is greater than the chosen number, inform the user that their guess is too higher
Otherwise, break out of the loop (essentially ending the code)
The only time when the 3rd condition would be fulfilled is if the number isn't too high, and isn't too low. The number is only both not too high and not too low if the number is equal to the chosen number. The else statement is what's causing your issues. Remove it.
Try this code out to see if it solves your problem:
import random
secret_number = random.randint(1, 20)
print("I'm thinking of a number between 1 and 20")
for guesses_taken in range(1, 7):
guess = int(input("Take a guess"))
if guess < secret_number:
print("This number is too low")
elif guess > secret_number:
print("This number is too high")
elif guess == secret_number:
print(f"Good job! You guessed my number in {guesses_taken} guesses!")
else:
break
if guesses_taken == 6:
print(f"Nope, the number I was thinking about was {secret_number}")
#this line won't display even when the number is guessed correctly
if guess == secret_number and guesses_taken < 6:
print(f"Good job! You guessed my number in {guesses_taken} guesses!")
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I tried to write a guessing game in python.. I wrote everything correct but still it's not showing even if I guess correct number
secret_number = 5
guess_count = 0
guess_limit = 3
k = 0
while guess_count < guess_limit:
K = input(print("guess the number"))
guess_count = guess_count + 1
if k == secret_number:
print('you won')
else:
print('try again')
Most of your problem is in your input line.
Your code
K = input(print("guess the number"))
Problems
python is case-sensitive so K is not k. You are making another variable named K and not assigning to existing k.
you are calling print inside input, so you are printing None (which print returned). That should be input("guess the number")
input returns a string, and later in if k == secret_number, it's not really True because '5' is not 5 in python.
Solution
Seeing all the problems, that line should be
k = int(input("guess the number"))
change the line, your program will work.
working code
secret_number = 5
guess_count = 0
guess_limit = 3
k = 0
while guess_count < guess_limit:
k = int(input("guess the number"))
guess_count = guess_count + 1
if k == secret_number:
print('you won')
else:
print('try again')
Multiple problems:
Your else statment isn't correctly indented, and your K is capital.
Here is a working solution:
#secret number
sn=5
#amount of guesses
gc=0
#guess limit
gl=3
k=0
while gc<gl:
k=int(input("Guess the number"))
gc+=1
if k==sn:
print("you won :)")
break
else:
print("Try again:(")
[EDIT]
Also, try using int(input()) instead of just input for number programs like this, as it makes it a proper number.
You also don't need a print statement inside of an input.
A string inside an input is printed as if print() was called
guessname= "Elephant".capitalize()
enterguessname=""
guescount=0
guesslimit= 3
outofguess= False
while enterguessname != guessname and not outofguess:
if guescount < guesslimit:
enterguessname = input("Enter guess name: ").upper()
guescount += 1
else:
outofguess= True
if outofguess:
print("you lose and correct name is " + guessname)
else:
print("Your answer is right and correct answer is " +
guessname)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am trying to make a random number guessing game but I cant get the if statement to check if the users input is = to the random number
import random
realNumber = random.randint(1, 50)
print(realNumber)
myNumber = print(input("Guess the number from 1 to 50: "))
if int(myNumber) == realNumber:
print("You win")
else:
print("Nope guess again")
The unintended behavior of your program is due to this line:
myNumber = print(input("Guess the number from 1 to 50: "))
Here, you are trying to assign myNumber to the return value of the print statement (Which is None) and not the value obtained from the input() statement. To fix this, simply remove the print() around the input.
myNumber = input("Guess the number from 1 to 50: ")
Hope this helped!
You don't need the print statement around input.
import random
realNumber = random.randint(1, 50)
print(realNumber)
myNumber = input("Guess the number from 1 to 50: ")
if int(myNumber) == realNumber:
print("You win")
else:
print("Nope guess again")
Note that this code will not work if the user enters something besides an integer, because the int() call will not cast correctly
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have this basic code and what I want it to do is choose a random number, then it would ask the user to input a number from 0 to 100, the would give back results based on the number, such as number is too low or high. I keep switching things around but always get an error, this for 2.7:
import random
randomNumber = random.randrange(0,100)
guess = -1
guess = int(input('Enter a number between 0 and 100: ')
guess != randomNumber:
if guess < randomNumber
print('Your guess is too low.')
elif guess > randomNumber:
print('Your guess is too high.')
print('You win!')
First, you define guess twice. Just define it once. There is no need to make guess be equal to -1 if that won't be the original value anyways. Second, I believe you want to use a while loop to keep checking if the user has guessed the correct number. In this case, add another guess into the while loop to allow the player to continue guessing and to keep proper syntax.
Don't let the player win always with another condition. Use colons after each if/elif statement as well. As you are comparing a string (the input) and an integer, change the input() to int(input()). The final code should be something like:
import random
randomNumber = random.randrange(0, 100)
guess = None # Define guess
while guess != randomNumber: # Create loop
guess = int(input("Enter a number between 0 and 100")) # Change guess
if guess < randomNumber:
print "Your guess is too low."
elif guess > randomNumber:
print "Your guess is too high."
elif guess == randomNumber:
print "You win!" # Win message under correct condition
break # Exit the loop as game is finished
You're missing a colon after your if statement. You're also printing You win! regardless of the outcome. Put that inside an else statement. You don't need this line guess != randomNumber:, and you don't need guess=-1, and finally you're missing a closing paren as mentioned in comments.
import random
randomNumber = random.randrange(0,100)
guess = None
while guess != randomNumber:
guess = int(input('Enter a number between 0 and 100: '))
if guess == randomNumber:
break
elif guess < randomNumber:
print('Your guess is too low.')
elif guess > randomNumber:
print('Your guess is too high.')
print('You win!')
As usual, commenting your code reveals the error instantly
import random
# Pick a number between 0 and 99
randomNumber = random.randrange(0,100)
# initialize guess (hint: why?)
guess = -1
# Ask the user to guess a number (hint: balanced parentheses help)
guess = int(input('Enter a number between 0 and 100: ')
# What is this line supposed to be? Looks like the beginning of a loop here, but what kind?
guess != randomNumber:
# If the guess is less than the number, then tell the user
if guess < randomNumber
print('Your guess is too low.')
# Otherwise if the guess is higher than the number, tell the user
elif guess > randomNumber:
print('Your guess is too high.')
# Regardless, win the game. Wait whuh..?
print('You win!')
Try the following code:
import random
randomNumber = random.randrange(0,100)
guess = int(input("Enter a number between 0 and 100:"))
if guess < randomNumber:
print('Your guess is too low.')
elif guess > randomNumber:
print('Your guess is too high.')
else:
print('You win!')
(EDIT - removed some unecessary lines)
missing some formatting, and there's a few lines (guess != randomNumber:) that don't really do anything (and in that case, the colon would cause a SyntaxError).
import random
randomNumber = random.randrange(0,100)
guess = int(input('Enter a number between 0 and 100: '))
if guess < randomNumber:
print('Your guess is too low.')
elif guess > randomNumber:
print('Your guess is too high.')
else: # assuming you only want to print this if they guessed right
print('You win!')
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
import random
number= random.randint(1,10)
count=0
guess=""
guess=int(input("please guess:")
while guess!= number:
if guess < number:
print("lower")
count++
elif guess > number:
print("higher")
count++
elif guess==number:
print("Good job, you got my number")
print("You got it in,",count,"tries")
FOr some reason when i try to run it, it says I have invalid syntax. Please help.
This is Python, not C or JS. This:
count++
Should be written as:
count += 1
Also, your keep in mind that Python is indentation-sensitive.
while guess!= number:
if guess < number:
print("lower")
count++
elif guess > number:
print("higher")
count++
elif guess==number:
print("Good job, you got my number")
print("You got it in,",count,"tries")
And lastly, you're missing a closing parenthesis:
guess=int(input("please guess:"))
Well, good luck learning!
You're missing an end parenthesis
guess=int(input("please guess:")
Should be:
guess=int(input("please guess:"))
Hope that helps
You also need to change your indenting on this:
elif guess==number:
print("Good job, you got my number")
print("You got it in,",count,"tries")
Also do increment correctly