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 2 years ago.
Improve this question
I have created a function named user_choice() using Jupyter Notebook. This function is expecting an valid integer input from the user and prints the same. If a user any input other than integer then it should display an error message like " Sorry that is not a digit!" and it will again ask the user to enter valid input.
Below is my code for the function user_choice()
def user_choice():
choice = "WRONG"
while choice.isdigit() == False:
choice = input("Enter a digit(0-10): ")
if choice.isdigit == False:
print("Sorry that is not a digit!")
return(int(choice))
On calling the above function and entering non integer value, It is not displaying the message "Sorry that is not a digit!"
Enter a digit(0-10): ten
Enter a digit(0-10):
Looks like you just forget brackets after .isdigit method in if statement.
You need to use recursive call
def user_choice(choice="WRONG"):
if choice.isdigit() == False:
choice = raw_input("Enter a digit(0-10): ") #use input for higher python version, mine is 2.7 hence used raw_input
if choice.isdigit() == False:
print("Sorry that is not a digit!")
user_choice(choice)
else:
print("Captured:"+choice)
return(int(choice))
A shorter version:
def user_choice(choice="WRONG"):
if choice.isdigit() == False:
choice = raw_input("Input must be a digit(0-10): ") #use input for higher python version, mine is 2.7 hence used raw_input
user_choice(choice)
else:
print("Captured:"+choice)
return(int(choice))
Related
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
When I run this easy program and want to exit it, I need to type exit 2 times (at line 7).
def registration():
def username_registration():
entering_username = True
while entering_username:
print("Type exit to leave the registration.")
usernam_from_registration = input("Enter username: ")
if usernam_from_registration == "exit":
break
lenght_usernam_from_registration = len(usernam_from_registration)
if lenght_usernam_from_registration > 15:
print("too long")
else:
return usernam_from_registration
username_registration()
print(username_registration())
registration()
Why is this and how can I make it so I only need to write it one time?
This is because you are calling the username_registration() function twice.
username_registration()
print(username_registration())
The first time you call it, nothing happens because you are not doing anything with the result.
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 2 years ago.
Improve this question
I am very new to Python, I have searched and cannot find answer. Thank you in advance.
I purposely type the incorrect answer and it responds properly - prompts me to give input again.
I now type the correct answer and it responds with error (my code is below error)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
in
54 print("Enter Y/N:")
55 answer = input()
---> 56 answer = input.upper()
57
58 print("Great! Lets get started.")
AttributeError: 'function' object has no attribute 'upper'
`# intro text
print("Are you ready to create a super hero with the super hero generator 3000?")
# Ask for input / use input() to take it / use upper() to adjust text
print("Enter Y/N:")
answer = input()
answer = answer.upper()
# adding while loop to condition input(answer)
while answer != "Y":
print("Sorry, but you have to choose Y to continue.")
print("Enter Y/N:")
answer = input()
answer = input.upper()
print("Great! Lets get started.") ```
Change your code to:
while answer != "Y":
print("Sorry, but you have to choose Y to continue.")
print("Enter Y/N:")
answer = input()
answer = answer.upper()
input() function has no function upper(), you want to call upper() on answer.
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 2 years ago.
Improve this question
I am a beginner learning Python and wrote the following codes. To control the loop flow I tried using a variable "test", but the loop never stopped. Appreciate if any thoughts.
test=False
while test==False:
a=input("Please enter an even number to make test True: ")
if int(a)%2==0:
test==True
print("test is now True")
else:
print("Please try again!")
test=False
while test is False:
a=input("Please enter an even number to make test True: ")
if int(a) %2 == 0:
test = True
print("test is now True")
else:
print("Please try again!")
Fixed the test == true, should've been test = true :) have fun learning
heres the right code:
test=False
while test==False:
a=input("Please enter an even number to make test True: ")
if int(a)%2==0:
test=True
print("test is now True")
else:
print("Please try again!")
here's what you did wrong:
in the 5th line test==True the == checks if that's what the variable true stores, it doesn't set it; so this can be simply fixed by test=True
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
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.
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.
Improve this question
I am trying to make a calculator. I have the code that does the operations working, but I am trying to allow the user to keep the calculator running without having to rerun the code. i an trying to assign a Boolean to variable, but python keeps telling me there is a name error, which is the the variable is not defined. Can you please help me?
Thank you in advance.
I have tried to change the available name but it doesn't do anything.
The code stopped working when it got to the while run == true line.
else:
print('Invalid operator, please run code again')
run = True
while run == True:
print(' do you need another problem solved? y/n')
if input() == y:
run = True
elif input() == n:
run = False
I expected the code to ask me if I need another problem solved, but there is a name error.
If the error is complaining about y or n it's because you need to surround it with quotes
if input() == "y":
run = True
Also, run == True is not needed
while run:
does the trick
else:
print('Invalid operator, please run code again')
run = True
while run:
print(' do you need another problem solved? y/n')
inp=input()
if inp == 'y':
run = True
elif inp == 'n':
run = False
you have used input 2 times so your code will wait 2 times for input, use input() once.
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
When I press "y" in the first question the loop just keeps going instead of stopping.
Done = True
while Done:
quit = str(raw_input ("Do you want to quit? "))
if quit == 'y' :
Done=False;
attack = str(raw_input("Does your elf attack the dragon? "))
if attack=='y':
print ("Bad choice, you died.")
done=False;
print "Loop stopped"
I am using Python 2.7.
You may want to use a break, this is used to stop a loop:
while True:
quit = str(raw_input ("Do you want to quit? "))
if quit == 'y' :
break # Add this
...
Quoting Python docs:
The break statement, like in C, breaks out of the smallest enclosing for or while loop.
Edit:
You could try using an infinite loop (while True) and when you want to exit it, just check for a condition and use a break statement.
Python is case-sensitive. You need to make sure Done is always capitalized or not:
>>> Done = True
>>> while Done:
quit = str(raw_input ("Do you want to quit? "))
if quit == 'y' :
Done = False
attack = str(raw_input("Does your elf attack the dragon? "))
if attack=='y':
print("Bad choice, you died.")
Done = False
print("Loop stopped")
As Makoto has pointed out, in Python 2.x, the parentheses in the print statements above are a grouping mechanism. But in Python 3.x, print constitutes a function and requires parentheses. The above code will work in both versions of Python.