Python assignment for school [closed] - python

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

Related

Why is there a SyntaxError Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
I wrote this code snippet:
lowestNumber = int(input("\nWhat would you like your lowest number to be?"))
highestNumber = int(input("What would you like your highest number to be?"))
number = random.randint(lowestNumber, highestNumber)
tries = 0
while tries < 10:
guess = int(input(f'\nEnter a number between', lowestNumber))
if guess == number:
print("You guessed correctly! The number was", number)
break
elif guess < number:
print("Too low!")
elif guess > number:
print("Too high!")
tries += 1
SyntaxError: bad input on line 22 in main.py.
Line 22 was guess = int(input(f'\nEnter a number between', lowestNumber)).
I searched it up on google and got nothing, I pasted it into OpenAI's code fixing and it also didn't help.
How can I fix this error?
When you wrote
guess = int(input(f'\nEnter a number between', lowestNumber))
it passed both the string and lowestNumber into the input function. However, you probably wanted to write something like Enter a number between (lowestNumber) and (highestNumber). To do this, you would have to write
guess = int(input(f'\nEnter a number between {lowestNumber} and {highestNumber}. '))
In my example, it passes in one object, the string, which contains lowestNumber and highestNumber in it. In your example, it passes in two objects, the string and lowestNumber.
The formatting you did in the input functions works in print statements, so the print statements are correct, but the input function is not.

Using random in an if statement [closed]

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

How to debug beginners code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have 2 questions about my code. Why the program doesn't go in the second if statement. How can I end the loop?
from random import *
SecretNumber=randint(1,5)
Guess=int(input("Please enter Guess: "))
NumberofGuesses=1
SecretNumber=0
while Guess != SecretNumber:
NumberofGuesses=NumberofGuesses+1
if Guess>SecretNumber:
print("Please insert a smaller number")
else:
print("Please insert a bigger number")
if Guess==SecretNumber:
print("Number of Guesses: {0}".format(NumberofGuesses))
Your second if is outside the while loop, so it won't get hit until you guesss the secret number. The loop never ends because you never read another guess.
You also have a problem that you are overriding your random secret number with zero.
You need something like:
import random
SecretNumber=random.randint(1,5)
NumberofGuesses=0
while true:
Guess=int(input("Please enter Guess: "))
NumberofGuesses += 1
if Guess == SecretNumber:
break # Got it!
elif Guess>SecretNumber:
print("Please insert a smaller number")
else:
print("Please insert a bigger number")
print("Number of Guesses: {0}".format(NumberofGuesses))
It's because you're setting SecretNumber to 0. Remove it and it should work.

How do I repeat the program? [closed]

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 was wondering how to reset this program, and I need help. I have been looking everywhere for an answer, but I can't find a program that works. Can someone please help me?
print("Answer These MATH Questions")
def program():
math = int(input("What Is 8 x 4: "))
if math == ("32"):
print("You Got The Question Correct")
else:
print("Sorry You Got The Question Wrong Try Again")
program()
return
Use while like example. Don't use int for input - maybe it will not number:
while 1:
math = input("What Is 8 x 4: ")
if not math.isdigit():
print("It's not number")
elif math == "32":
print("You Got The Question Correct")
break
else:
print("Sorry You Got The Question Wrong Try Again")
change if math == ("32"): to if math == 32:

Simple Python Code Invalid Syntax [closed]

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

Categories