Home > Mobile >  why can't i use the user's input to give a "true" result?
why can't i use the user's input to give a "true" result?

Time:08-18

I have started learning python around a week ago, so i'm still learning. I'm making a game (really basic one), and at the start i ask the user if they want to start playing.

go = input("")
if go != "yes" or go != "sim":
 print(':(')
else:
 print(':)')

But for some reason, it always prints a sad face. It has worked the first time i made it (i was forced to restart the whole thing once), yet i don't feel i actualy did anything different and it is driving me mad! There are many other problems that i feel the code is better at explaining. (be aware i only know the bare basics)

print('''___________________________________________________________________________________________
|                                                                                         |
|                                                                                         |
|   \        /\        /  ______   |        _________  __________  |\        /|  ______   |
|    \      /  \      /   |        |        |          |        |  | \      / |  |        |
|     \    /    \    /    |_____   |        |          |        |  |  \    /  |  |_____   |
|      \  /      \  /     |        |        |          |        |  |   \  /   |  |        |
|       \/        \/      |_____   |______  |________  |________|  |    \/    |  |_____   |
|                                                                                         |
|_________________________________________________________________________________________|

start game? (começar jogo?)''')

#necessary variables

has_dagger = False
has_pole = False
unnarmed = True
is_dead = False
wallet = 0

#misc variables
go = "a"
destination = ""
action = "bruh"

#main map

go = input("")
if go != "yes" or go != "sim":
  #???
  print(':(')
else:
  print(':)')
while 1 == 1:
 print("""            ^ 
           /.\ -- icy peaks (picos gelados)
     ^    /  .\ 
    / \  /   . \ 
   / . \/   .   \ ^
  /   . \  .     /.\               *
 /  .    \.     /.  \             *
/____.____\____/_.___\_____        *
  ~   ~       ~ ~   ~   ~   
    ~      ~        ~     ~       *
  ~        ~    ~     ~  High Seas *
   ~    ~        ^  (maré alta) __
   ^      ^     /_\ / \        [  ] 
  / \    /_\^ ^ / \/___\  ______||__
 /_ _\   / \ /_\   \   \ _/__/__/__/\ 
 /   \ ^/___\   \___\___\/__/__/__/  \ 
/_  __\/_  __\  _\ _\  _\         |[=]
__||_____||___||_||__||___[]   [] || |
                          ________||_|
   ^- Forest (floresta)      ^-Shop (loja)
  
 type where you want to go (digite onde você quer ir)""")
 
 #shop
 
 #this while loop is not stoping
 while action != "exit" or destination != "sair":
  destination = str(input(''))
  #this if statement has the same problem as the start
  if destination != "shop" or destination != "loja":
   print('invalid answer! (resposta inválida)')
  print("""
   ^     ^___^                                                      ________ 
   ) )  / ● ● \   Meow                                             |        | 
  / /   \  W  /                                                    |   |\   | 
 / /    / || |                                                     |   ;|   | 
( (    /| || |                                                     |  _|¡_  | 
 \ \  / | || |                                                     | (____) | 
__\ \/ /| || |______________________________                       |   ||   | 
   \/__/| |_>|_>                           /\                      |   ()   | 
                                          / /                      |________| 
_________________________________________/ /                         ___|___
_________________________________________\/                         | rusty | (adaga usada)
                                                                    | dagger| 
                                                                    | 50$   | 
                                                                    |_______| 
                ()                             ()
 _______________()_____________________________()_________________
()_____________()_____________________________()_________________() 
                ()             |               ()
                            ___|___ 
                           |wooden | (vara de madeira)
                           |stick  | 
                           | 150$  | 
                           |_______|
    Type what you want to buy (digite o que você quer comprar)
Or type "exit" to go to map (ou digite sair para voltar ao mapa)
""")

#unusable for now

action = input('')
if action == "wooden stick" and wallet >= 150 or action == "vara de madeira" and wallet >= 150:
  has_pole = True
  wallet -= 150
elif action == "rusty dagger" and wallet >= 50 or action == "adaga usada" and wallet >= 50:
  has_dagger = True
  wallet -= 50
else:
  print('invalid answer! (resposta inválida)')

CodePudding user response:

you need to change or to and:

go = input("")
if go != "yes" and go != "sim":  # use `and` instead of `or` here
 print(':(')
else:
 print(':)')

Or another option, could be to flip the smiley faces and update != to ==:

go = input("")
if go == "yes" or go == "sim":
   print(':)')
else:
   print(':(')

Note that your if condition could be slightly simplified using an in operator too:

if go.lower() in ('yes', 'sim'):

This will also handle different-cased inputs from the user, like "YES" or "Sim".

CodePudding user response:

The problem is that we have a conditional that will always be True, in this case go can either be yes, sim or other. So let us look at at each, if go = 'yes' then we enter because go != 'sim'. If go = 'sim' then we enter because go != 'yes', and finally if go = other, we enter because again go != 'yes' (or sim but the former will be evaluated first).

So let us rearrange our conditional so it is working as expected:

go = input("")
if go == "yes" or go == "sim":
 print(':)')
else:
 print(':(')

We now default to ':(' when go is not 'yes' or 'sim', otherwise we print ':)'.

  • Related