-3

I am very new to python and was wondering why this code does not work , I am pretty sure it is my incorrect use of or, however I do not quite understand how ia m supposed to use it. Here is my simple code

continue_shopping=int(input("press 0 to stop shopping and print your reciept or press 1 to continue shopping"))
    if continue_shopping !=0 or !=1:
        print("make sure you enter a valid number")
Yeas123
  • 1
  • 2
  • Did you meant continue_shopping != 0 **and** != 1. With or it will always enter that if statement. Since it can not be 0 and 1 at the same time. – umutto Mar 24 '17 at 07:33
  • So basically if i type in 0 , it would say make sure you enter a valid number , if i put in any random number it says make sure you enter a valid number – Yeas123 Mar 24 '17 at 07:34
  • Ah , I could use and , now i get a invalid syntax error , the equals sign is highlighted in red – Yeas123 Mar 24 '17 at 07:35
  • Please provide the actual error message. There are multiple issues in that code, but I cannot guess which one is due to bad copy/pasting and which is the actual issue. – Jonas Schäfer Mar 24 '17 at 07:35
  • It says invalid syntax , the equals sign is highlighted in red – Yeas123 Mar 24 '17 at 07:36
  • try `if continue_shopping != 0 and continue_shopping != 1:` to fix that syntax error. Also your indentation is wrong, no need for indentation after first line. – umutto Mar 24 '17 at 07:37
  • Without the if? – Yeas123 Mar 24 '17 at 07:38
  • continue_shopping=int(input("press 0 to stop shopping and print your reciept or press 1 to continue shopping")) continue_shopping != 0 and continue_shopping != 1: print("make sure you enter a valid number") , i get a invalid syntax at the end of the second line – Yeas123 Mar 24 '17 at 07:39
  • right, with the if. – umutto Mar 24 '17 at 07:39
  • Now it just runs that line as the beggining line of the code – Yeas123 Mar 24 '17 at 07:39
  • and look for a basic programming tutorial, the one that talks about syntax and stuff. – umutto Mar 24 '17 at 07:40
  • Possible duplicate of [How do I test one variable against multiple values?](http://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values) – SiHa Mar 24 '17 at 08:42

3 Answers3

4

if continue_shopping !=0 or !=1: Is invalid syntax.

You have to write if continue_shopping !=0 or continue_shopping !=1:

Because isolated !=1 cant be evaluate into False or True but continue_shopping !=1 can.

However you should take a look at or in your condition. You probably wanted to use and.

Erik Šťastný
  • 1,487
  • 1
  • 15
  • 41
1

Edit your code to:

if continue_shopping != 0 and continue_shopping != 1:

When it is if continue_shopping !=0 or !=1: python reads it as if (continue_shopping !=0) or (!=1): which isn't the expected result for you

0
  1. With Python be careful with proper indenting. Take a look here: Indenting Code - Dive Into Python

  2. You missed another or compare on variable, below is reapired code:

    if continue_shopping !=0 or continue_shopping !=1:
       print("make sure you enter a valid number")
    
  • This is caused a strange sensation in my code, these three lines of code work , however they are the first lines that are run in my code when they are in the very end. Do you know why this could be? – Yeas123 Mar 24 '17 at 07:46
  • `continue_shopping != 0 or continue_shopping !=1` is always `True`. – bereal Mar 24 '17 at 07:50
  • Yes,agree. But maybe he wants to enter some other values too – wishmaster75 Mar 24 '17 at 07:52
  • May i ask why my answer is not useful? – wishmaster75 Mar 24 '17 at 07:57
  • 1
    Because it contains an obvious logical mistake. – bereal Mar 24 '17 at 08:03
  • He was asking why his code isn't working. He did wrong identitation on code, and logical operators were wrong. Ive just answered what he was asking, logical or not, the code is working now. Logical problems are off the scope of this question. – wishmaster75 Mar 24 '17 at 08:18