Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A text-based game [SOLVED]
#1
I am making a text-based game and I am trying to run this code but the last two lines don't work and any number including 1 2 and 3 are printing enter 1 2 or 3. Please help me with this I am kind of new to python.

the code:
num = input('''-Write a number between 1 to 3 to ask the parallel question. Don't forget to click enter after you enter the number!-.
        1. Why are we going there?
        2. Can I just go back home?
        3. I am done\n''')
elif int(num) != 1 or int(num) != 2 or int(num) != 3:
    print('enter 1 2 or 3')
    exit()
buran write Apr-20-2021, 12:26 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
this code will produce SyntaxError, because there is elif without if

Error:
File "***", line ** elif int(num) != 1 or int(num) != 2 or int(num) != 3: ^ SyntaxError: invalid syntax
Then, even if you fix it, this condition int(num) != 1 or int(num) != 2 or int(num) != 3: is always true, because you use or
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Apr-20-2021, 12:29 PM)buran Wrote: this code will produce SyntaxError, because there is elif without if

Error:
File "***", line ** elif int(num) != 1 or int(num) != 2 or int(num) != 3: ^ SyntaxError: invalid syntax
Then, even if you fix it, this condition int(num) != 1 or int(num) != 2 or int(num) != 3: is always true, because you use or

ok, I changed it to if but do you have any idea on how to make the condition not always true?
Reply
#4
And, not or. Every number is eihter not 1 or not 2

But not in is better.
if num not in ('1', '2', '3')
Reply
#5
or
if num not in '123':
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Except that will also accept 12, 23 and 123
buran likes this post
Reply
#7
(Apr-20-2021, 01:56 PM)deanhystad Wrote: Except that will also accept 12, 23 and 123
Good catch
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Color a table cell based on specific text Creepy 11 1,825 Jul-27-2023, 02:48 PM
Last Post: deanhystad
  [SOLVED] [BeautifulSoup] How to get this text? Winfried 6 1,926 Aug-17-2022, 03:58 PM
Last Post: Winfried
  Delete empty text files [SOLVED] AlphaInc 5 1,510 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  [SOLVED] [ElementTree] Grab text in attributes? Winfried 3 1,584 May-27-2022, 04:59 PM
Last Post: Winfried
  select Eof extension files based on text list of filenames with if condition RolanRoll 1 1,475 Apr-04-2022, 09:29 PM
Last Post: Larz60+
  Extracting Specific Lines from text file based on content. jokerfmj 8 2,857 Mar-28-2022, 03:38 PM
Last Post: snippsat
  [SOLVED] Read text file from some point till EOF? Winfried 1 1,911 Oct-10-2021, 10:29 PM
Last Post: Winfried
  Extract text based on postion and pattern guddu_12 2 1,580 Sep-27-2021, 08:32 PM
Last Post: guddu_12
  Sorting and Merging text-files [SOLVED] AlphaInc 10 4,753 Aug-20-2021, 05:42 PM
Last Post: snippsat
Thumbs Up [SOLVED] Find last occurence of pattern in text file? Winfried 4 4,305 Aug-13-2021, 08:21 PM
Last Post: Winfried

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020