Reputation: 1
so doing this for my stage 1 computer science paper. following code is what i've written atm. Line 16 (while statement) is giving a syntax error. The book asks us to
1) prompt the user to enter a guess and store the value in the "guess" variable,
2) if guess is greater than goal print...
3) if guess is lower than goal... print...
4) if guess is same as goal, print...
Unsure how to fix this. Any help will be greatly appreciated. Code as below.
#Author: Anuj Saluja
#Date: 17 October 2016
import random
goal = random.randint(1,100)
guess = 0
print ("The object of this game is to")
print ("guess a number between 1 and 100")
print()
inputguess = int(input("Please guess the number: ")
while (guess != goal):
if inputguess > goal
print ("Too high, try again.")
if inputguess < goal
print ("Too low, try again.")
if inputguess == goal:
break
if inputguess == goal:
print ("Well done!")
print ("See you later.")
Upvotes: 0
Views: 67
Reputation: 1
So I had someone help me irl and this works perfectly for anyone interested. Thanks everyone for help. :) Appreciate it.
goal = random.randint(1,100)
guess = 0
print ("The object of this game is to")
print ("guess a number between 1 and 100")
print()
while guess != goal:
guess = int(input("Please guess the number: "))
if guess > goal:
print ("Too high, try again.")
if guess < goal:
print ("Too low, try again.")
if guess == goal:
break
if guess == goal:
print ("Well done!")
print ("See you later.")
Upvotes: 0
Reputation: 2781
I think you are looking for this. hope this will work.
import random
goal = random.randint(1,100)
guess = 0
print ("The object of this game is to")
print ("guess a number between 1 and 100")
inputguess = int(input("Please guess the number: "))
while True:
if inputguess > goal:
inputguess = int(input("Too high, try again: "))
elif inputguess < goal:
inputguess = int(input("Too low, try again: "))
elif inputguess == goal:
print ("Well done!")
print ("See you later.")
break
Upvotes: 1
Reputation: 5818
Have you read your stack trace in detail? It's very easy to just skim over them, but stack traces can actually provide a lot of very useful information. For instance, the message is probably complaining that it was expecting a closing parenthesis. The line:
inputguess = int(input("Please guess the number: ")
should actually be
inputguess = int(input("Please guess the number: "))
The stack trace says the error is on line 16 because that's where the interpreter realized something was wrong. More often than not, the buggy code will be in the last line of code before the line it gives you.
also as other people stated, you need to put the input statement within the while loop in order to update the variable.
You should also consider replacing tab characters with 4 spaces so it displays consistently no matter where you read it. Usually the text editor or IDE will have tab settings that you can change when you hit tab, it will type 4 spaces. A quick Google search along the lines of "{text editor} change tab settings" will usually bring up results on how to change it, where {text editor} is the name of the editor/IDE you're using.
You should also consider indenting the code within your if statements as it improves readability
Upvotes: 0
Reputation: 150
Put this line :
inputguess = int(input("Please guess the number: ")
inside the while loop. The code is only asking the user input once, user input must be in the loop.
Upvotes: 0
Reputation: 11
The code is only asking for a guess once before the while loop. You need to update the guess variable inside the while loop by asking for input again.
Upvotes: 1