1
size = input("What size pizza do you want? S, M, or L ")
addpepperoni = input("Do you want pepperoni? Y or N ")
extracheese = input("Do you want extra cheese? Y or N ")

S = 15 
M = 20
L = 25 
N = 0

if size == S: 
    if addpepperoni == N:
        if extracheese == N:
            bill = S 
            print ("cool")

Just testing it out to see if anything prints but nothing prints, not even an error. What am I doing wrong here?

tdelaney
  • 73,364
  • 6
  • 83
  • 116
Gadrri
  • 41
  • 5
  • 2
    `input()` returns strings; you are coming them to numbers. It also looks like you are asking for letter inputs like `"S"` but then comparing to `15`. That seems a little *unusual*. And you set `bill = S` with `S = 15`. Now `bill == 15`. Not sure what you're trying to do. – Mark Jul 17 '21 at 20:14
  • 1
    thank you, i know exactly what I'm doing wrong. – Gadrri Jul 17 '21 at 20:17
  • 1
    just learning how to code, thats why it looks like a mess – Gadrri Jul 17 '21 at 20:18
  • 1
    Fair enough. It helps to print things out as you learn. For example adding `print(size, S)` before your `if` will show you why they are not equal. – Mark Jul 17 '21 at 20:19
  • 3
    @Mark has a good suggestion, but `print(repr(size), repr(S))` may be better because the "repr" representation of an object typically has more information than the "str". Your string, for instance, would be `'S'` (quotes included) which gives more of a type hint. – tdelaney Jul 17 '21 at 20:20

4 Answers4

1

The problem with this code is here:

if size == S: 
    if addpepperoni == N:
        if extracheese == N:

You have assigned S to 15 and N to 0, so what the code thinks you want to do is:

if size == 15: 
    if addpepperoni == 0:
        if extracheese == 0:

This is because you are checking if the variable size is equal to the variable S. A fix to this would be making it a string, for example:

if size == 'S': 
    if addpepperoni == 'N':
        if extracheese == 'N':

Another thing you can do to improve your code is to use and, to check multiple conditions with only one if statement, for example:

if size == 'S' and addpepperoni == 'N' and extracheese == 'N':

This would now check if size is equal to the string S and not the variable S, and the same for the other two. For more info on data types, go here!

Sam
  • 21
  • 7
0

I guess you want to give the inputs 'S' and 'N', right? Then you should compare them to string and not to the integer values stored in the variables.

if size == 'S'
      if addpepperoni == 'N':
        if extracheese == 'N':
          bill = S 
          print ("cool")
signedav
  • 146
  • 1
  • 4
  • 1
    In python 3.x, `input` always returns a string. Since `"15"` doesn't equal `15`, it still won't work. – tdelaney Jul 17 '21 at 20:23
  • Yeah, you're right. But since the questioner wants to input 'S' etc. it has no influence to the sollution. But I'll correct it. – signedav Jul 17 '21 at 20:29
0

I think you misunderstand the concept of datatypes here. When you use an IDE to debug this code and put a breakpoint at the line with **if size == S: ** you would see that size has the value (for example) 'S' (which is the character typed into your terminal. But you are comparing it with S which is, in the context of your program, a variable that holds the value 15.

Since the Character 'S' and 15 are not equal your program will not run the following if checks and stop.

To fix this you should compare the size value which holds the character 'S' with the character 'S'. So, your check would be: **if size == 'S'**.

The same problem applies to the following checks you wrote.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Semkado
  • 25
  • 1
  • 5
0

This is probably what you want in the specific example given but you would have to change it if you want combinations of outcomes.

This will only give a result of 'cool' if ALL conditions are met.

Ideally you would probably want classes here too.

size = input("What size pizza do you want? S, M, or L ")
addpepperoni = input("Do you want pepperoni? Y or N ")
extracheese = input("Do you want extra cheese? Y or N ")

S = 15
M = 20
L = 25
N = 0

if size == "S" and addpepperoni == "N" and extracheese == "N":
    bill = S 
    print ("cool")
SimonT
  • 974
  • 1
  • 5
  • 9