Python ignoring if

Viewed 56

Starting with python(and with computer programming) I'm trying to code here and I'm not sure why python is ignoring my if/elif. I'm creating a variable with a random number and the user must guess for every wrong attempt, I'm giving a hint (try a lower/bigger number) once the user inserts the right number, I'm trying to create a continue option. But once the user is prompted for the question Você quer jogar novamente? (do you wanna play again?), independently of the input, the game always go for the first option of the next if.. I'm trying to implement a Yes or no input(it's ok) and using the answer to close the application or starting the game again (not ok).

any thoughts?

thxxxxx

import random
import os
import time

# escolhendo o numero
numberSelected = random.randrange(101)


def dica():
    if number > numberSelected:
        print("Tente um numero menor")
    else:
        print("Tente um numero maior")


def cls():
    os.system('cls' if os.name == 'nt' else 'clear')


while True:
    text = input("Aperte enter para começar...")
    if text == "":
        cls()
        print(
            "Um número de 0 a 100 foi gerado\nagora você precisa adivinhar o numero gerado")
        break
    else:
        print("Você ainda não apertou o Enter")
        cls()

number = None

while True:
    try:
        print(numberSelected)
        number = int(input('Chute um numero:'))
        if type(number) == int:
            if number == numberSelected:
                print("Muito bem você acertou!")
                print("Você quer jogar novamente?")
                retry = input('S/N?')
                if retry == "N" or "NAO":
                    print("Obrigado por jogar!")
                    exit()
                elif retry == "S" or "SIM":
                    print("vamos jogar novamente")
                    cls()
                    numberSelected = random.randrange(101)
                    continue
            elif number != numberSelected:
                cls()
                dica()
                continue
    except ValueError:
        print("Digite um numero Inteiro!!")
        time.sleep(0.8)
        cls()
1 Answers

It kind of sucks but python does not allow you to do this.

if answer == 'no' or 'yes'

That will raise an error. So instead you have to do this.

if answer == 'no' or answer == 'yes'

And alternative could be using in

if answer in ('no','yes)

fixed up code

if number == numberSelected:
    print("Muito bem você acertou!")
    print("Você quer jogar novamente?")
    retry = input('S/N?')
    if retry in ("N","NAO"):
        print("Obrigado por jogar!")
        exit()
    elif retry in ("S","SIM"):
        print("vamos jogar novamente")
        cls()
Related