1

I have an else if statement that will ask the user if they would like to scale the a number. If the answer is No then I want the scaler to be 0, while if the answer is yes, I would like the user to input the scaler. Every time I run the yes scaler, it still detects that the scaler is 0 and never lets the user input a number. Any suggestions?

Here is my code and output:

print ("Do you want to scale the price, Yes or No?")
answer = input("")

if answer == 'no' or 'No':
    scale = 0
elif answer == 'yes' or 'Yes':
    print("How much would you like to scale it by?")
    scale = float(input ())


print (scale)

output:

Do you want to scale the price, Yes or No?
 Yes
0
apple1234
  • 11
  • 2

4 Answers4

0

If you break apart your first if statement, you can rewrite what you have as:

if (answer=='no') or 'No':

Because 'No' is a non-empty string, it will always evaluate to True. So this block will always be executed.

To fix this, use in:

if answer in ('no', 'No'):
    scale = 0
elif answer in ('yes', 'Yes'):
    print("How much would you like to scale it by?")
    scale = float(input ())
James
  • 32,991
  • 4
  • 47
  • 70
0

You need to check the answer for both "no" and "No", similarly for both "yes" and "Yes".

Make the following changes in your code:

print ("Do you want to scale the price, Yes or No?")
answer = input("")

if answer == 'no' or answer == 'No':
    scale = 0
elif answer == 'yes' or answer == 'Yes':
    print("How much would you like to scale it by?")
    scale = float(input ())


print (scale)

Output:

Do you want to scale the price, Yes or No?
yes
How much would you like to scale it by?
5
5.0
Vishwa Mittar
  • 378
  • 5
  • 16
0

Simple fix:

print ("Do you want to scale the price, Yes or No?")
answer = input()

if answer == 'no' or answer=='No':
    scale = 0
elif answer == 'yes' or answer== 'Yes':
    print("How much would you like to scale it by?")
    scale = float(input ())


print (scale)

'no' or 'No' would result in yes and that would be compared with answer, so that would always be true. So change it by two statement with or

Kalyan Reddy
  • 326
  • 1
  • 8
-2

Try this: if answer == 'no' or answer == 'No': ... elif answer == 'yes' or answer == 'Yes':

edtheprogrammerguy
  • 5,957
  • 6
  • 28
  • 47
  • Re [this question](https://stackoverflow.com/q/75855797/5211833) you just voted to close as spam: please don't do that. Instead, simply use a red spam flag (top flag in the flag dialogue). That way, the post is deleted quickly (once it reaches 6 red flags) and trains the automated spam blocking system. Closing a question for being spam does neither of these things and keeps it visible to everyone. Thus, please do flag such posts as spam in the future. – Adriaan Mar 27 '23 at 12:38