Why is there a SyntaxError Python [closed] - python

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.

Related

Program that prompts the user for a non-negative integer n, and then writes a sum of the even digits of n [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 2 years ago.
Improve this question
I want a program that sums the even numbers of a bigger number using while function.
Exemple :
Number is : 12345
Print : 6 (2+4)
This is what i wrote so far:
num = int(input("Introduce a non negative number: "))
if num % 2 == 0:
sum += num
print("sum")
I can't stress this enough
When doing school assignments, the whole idea with assignments is to teach you concepts, not solutions. There's a reason why you were given this assignment, asking others to solve it for you - is not the way to go.
Go back to your teacher, and ask for help if something is unclear. But because others start posting solutions I might as well just keep mine here.
Skipping the conversion to an early integer, will allow you to iterate over it as a string, and grab one number a a time.
num = input("Introduce a non negative number: ")
total = 0
for i in num:
if int(i) % 2 == 0:
total += int(i)
print("sum:", total)
You can then use your original logic, with some minor modifications.
Since for whatever reason, you're only allowed to use while and not for, you'd have to just adapt a bit.
num = input("Introduce a non negative number: ")
total = 0
i = 0
while i < len(num):
if int(num[i]) % 2 == 0:
total += int(num[i])
i += 1
print("sum:", total)
While I'm at it, after reading my code again. I am quite sure that a while loop here is the least pretty solution to this problem. But from a teaching standpoint there might be some benefit here. But I'd recommend going with the for loop if at all possible.
Try this one:
# split the number to list of digits
digits_list = list(input("Introduce a non negative number: "))
total = 0
# loop over the digits list and pick a digit till the list is empty
while digits_list:
digit = digits_list.pop()
if int(digit) % 2 == 0:
total += int(digit)
print(total)

Looping a whole python function until true [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 years ago.
Improve this question
I'm new to python and I want to archive a function as below where a input of a user gets checked with the arithmetic operators against the case of the random typed number being true to the statement number < 100 and when not being looped by the elif function.
So I want to check the first if statement but if that is not true it should go to the elif statement and then be checked by the if statement again until it fits the criteria.
def unit(number):
if number < 100:
a = round(((number/10)-(number//10))*10)
return a
elif number => 100:
number/10
userInput = int(input("Your number please: \n"))
print(unit(userInput))
SOLVED!
I could solve the problem by doing the following changes:
userInput = int(input("Your number please: \n"))
def unit(number):
if number < 100:
a = round(((number/10)-(number//10))*10)
return a
elif number >= 100:
b = (number/10)
return unit(b)
print(unit(userInput))
use recursion
def unit(number):
if number < 100:
a = round(((number/10)-(number//10))*10)
return a
elif number => 100:
return unit(number/10)
userInput = int(input("Your number please: \n"))
print(unit(userInput))

How to limit the number of digits input? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am requesting someone to tell me a program that asks a user to input only 9 digits. Number 0 not at start but having 9 digits.
How can I limit the number of digits?
For example:
Num=int(input('please Enter a Number'))
if num?!
Please help me soon.
If i got this right this should work:
a=input()
if len(a)==9 and a[0] != '0' and a.isdigit():
#if you want it to be integer
a=int(a)
do something
Hope it helps.
Well if I understood correctly you need a program that asks the user to enter exactly 9 digits. In that case it's pretty simple.
i = input("Enter a nine digit number: ")
if len(i) != 9:
print("You must enter 9 digits!")
quit()
if not i.isdigit():
print("Your input must be a number!")
quit()
# num is a 9 digit integer
# i is a 9 digit string
num = int(i)
You can do this.
num = input('Please enter a number')
if not num[0] == "0" and len(num) == 9 and num.isdigit():
# You can print a message or cast the num into an integer here
# like this: input_number = int(num)
print(True)
else:
print(False)
So what's happening here is that the program will accept the user input and evaluate if the first digit is NOT 0 AND that the length is exactly 9 characters.

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.

Categories