mercredi 22 août 2018

custom exit function doesn't work properly - python 3.6

I made a custom exit function that will ask user if he wants to quit. There are two choices: y/Y for "yes" and n/N for "no". Every time i call this function and choose anything, it only uses first if statement and runs it (i think) without even checking it. I replaced if and elif statements bodies and it always uses first statement after input.

import random
import time
from sys import exit

a = random.randint(1, 5)
b = random.random()

def all_exit():
inp = (input("Do you really want to quit? Y/N: ")
if inp == 'y' or'Y':
    exit()
elif inp == 'n' or 'N':

    for i in range(a):
        print('game is restarting...')
        time.sleep(b)

    print('game restarted in about', round(a * b, 3), 'seconds\n')

else:
    print("Value error, try again.")
    all_exit()

Second thing, if i use type conversion of input to integer and use numbers 1 and 2 for if and elif statements, it works fine as i expect it to work.

import random
import time
from sys import exit

a = random.randint(1, 5)
b = random.random()

def all_exit():
inp = int(input("Do you really want to quit? Y/N: "))
if inp == 1:
    exit()
elif inp == 2:

    for i in range(a):
        print('game is restarting...')
        time.sleep(b)

    print('game restarted in about', round(a * b, 3), 'seconds\n')
    decision()
else:
    print("Value error, try again.")
    all_exit()

I need an explanation. Why it doesn't work properly in the firs example but everything is fine in the second example where i was using numbers?

Aucun commentaire:

Enregistrer un commentaire