This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 2 years ago.
How do i get this program repeat when 'again' gets 'Y' or 'y'? Yesterday the same code worked smh, but today it closes the program whatever i write in there) And yeah...tabulation is wrong but it's because stackoverflow copied it in some weird way :))
while True:
start = input("What do you want to do? + - * / ")
if start == '+':
x = float(input("digit 1 "))
y = float(input("digit 2 "))
res = x + y
print('The result is ' + str(res))
again = input('Do u want to try again? Y/N ')
if again == 'N' or 'n':
break
Look, you are using the wrong syntax for checking the condition. Use this syntax :
if again=='N' or again=='n':
break
Related
This question already has answers here:
How do I do a case-insensitive string comparison?
(15 answers)
Closed 6 days ago.
I am trying to create a small bit of code in Python that runs a conversation, then asks a Y/N question i.e "Do you believe the sky is blue?"
When I run the code, it works well until I reach the question. It ignores my parameters for Y/N answers to print specific responses. It gives me the print response attached to my "else" statement.
I am confused if I am writing my If/elif/else statements wrong?
My code is written as follows:
x = input('do you believe the sky is blue')
if x == "yes":
print("I believe you are correct")
elif x == "no":
print('I think you have a unique perspective.')
else:
print('Please answer Yes or No.)
You use .lower() to obtain a lower case version of a string, and you are missing a ' in the last print():
x = input('do you believe the sky is blue ').lower()
if x == "yes":
print("I believe you are correct")
elif x == "no":
print('I think you have a unique perspective.')
else:
print('Please answer Yes or No.')
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 4 months ago.
I'm trying to understand how to return to the top of a Python script from an else statement.
print('lets do some math!')
math_operation = input('Which would you like to start with, addition, subtraction or division? ')
if math_operation == "addition":
input_add1 = int(input('First number please '))
input_add2 = int(input('Second number please '))
result = input_add1 + input_add2
print(f'{input_add1} + {input_add2} = {result}')
elif math_operation == "subtraction":
input_sub1 = int(input('First number please '))
input_sub2 = int(input('Second number please '))
result = input_sub1 - input_sub2
print(f'{input_sub1} - {input_sub2} = {result}')
else:
print('I did not quite get that, lets try again')
input_div = int(input('now provide a number that is divisible from the answer'))
answer = result / input_div
print(answer)
You need to put this inside a loop,
math_operation= None
print('lets do some math!')
while math_operation != 'quit':
math_operation = input('Which would you like to start with, addition, subtraction or division? or quit')
... your code ...
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 4 years ago.
import random
while True:
dice_number = random.randint(1, 6)
print('You rolled ' + str(dice_number))
print("Do you wish to continue? [Y/n]" )
if input() == 'Y' or 'y':
pass
else:
break
I just wasted half an hour trying to figure this out, still don't know what I did wrong. Can anyone help me please
You can't do or == 'y'. You have to put the whole logic expression again. Like this:
import random
while True:
dice_number = random.randint(1, 6)
print('You rolled ' + str(dice_number))
print("Do you wish to continue? [Y/n]" )
a = input()
if a == 'Y' or a == 'y':
pass
else:
break
This question already has answers here:
How can I break out of multiple loops?
(39 answers)
Closed 7 years ago.
I've only been using Python for a few months and I'm a bit stuck. This is a chunk of a longer code:
while True:
method=input('''How would you like to analyse your data?
1 = mean
2 = quartile
3 = mode
4 = range
5 = variance
6 = standard deviation
''')
if method == '1':
mean=sum(theList)/len(theList)
print('The mean of this data set is '+str(mean)+'.')
while True:
moveOn=input('Calculate another measure? Y/N ')
if moveOn == 'Y' or moveOn == 'y':
print('Redirecting...')
time.sleep(1)
break
elif moveOn == 'N' or moveOn == 'n':
print('Thank you for using the PDAP. ')
break
else:
print('Invalid response. ')
The problem is, I need the elif option to break out of the first while loop, but also out of the second. If someone types 'n', I need to program to just finish there and stop completely, but I can't seem to work out how to do that.
You could add a variable to break the loop, kinda like this:
_run = True
while _run:
while True:
[... do something ...]
_run = False
break
Or, if you want to just quit your program, you could do this directly by calling sys.exit().
This question already has answers here:
raw_input without pressing enter
(11 answers)
Closed 9 years ago.
For instance I would like it so,
x = int(input("Please enter your guess :")
If x = 1:
Print ("correct")
Although each time i need to press enter, how could I make it so it would read on key press.
I did find getch() although i could not get it to work.
In Windows you can use msvcrt. On other systems its a bit more difficult, take a look at this recipe: http://code.activestate.com/recipes/134892/
You can use it that way:
getch = _Getch()
print 'Please enter your guess: '
x = getch()
if (int(x) == 1):
print 'correct'
else:
print 'wrong'