vendredi 11 décembre 2020

Have couple mistakes in python with while and elif statements

from random import randint

play = "yes"
while play == "yes":
    choice = int(input("Would you like to play yourself(1) or let machine(2) play? press '1' or '2': "))

    if choice == 1:
        print("You chose to play yourself")
        num = randint(1,9999)
        counter = 0
        while True:
            num_guess = int(input("Guess the number: "))
            if num_guess == 0:
                print("Player has left the game")
                break
            else:
                if num_guess > num:
                    print("Your guess is too high")
                elif num_guess < num:
                    print("Your guess is too low")
                elif num_guess == num:
                    print("Yay,you found it")
                    print("It took you " + str(counter) + " tries to guess the number")
                    break
            counter += 1

    elif choice == 2:
        print("You chose to let the machine play")
        your_num = int(input("Enter your number: "))
        lowest = 1
        highest = 9999
        counter = 0
        machine_guess = randint(1,9999)
        if your_num == 0:
            you_sure = input("Are you sure you want to leave the game? yes or no: ")
            if you_sure == "yes":
                print("Player left the game")
                break
        else:   
            while True:
                print("My guess is ",machine_guess) 
                is_it_right = input("Is it too small(<) or too big(>) or machine found(=) the number?: ")
                if is_it_right == ">":
                    if machine_guess > your_num:
                        highest = machine_guess
                        counter += 1
                    else:
                        print("!!!Don't cheat!!!")
                        your_number = input("What was your number?: ")
                        print(str(machine_guess) +" < " + str(your_number) + ",so you should have written '<' instead of what you wrote.Continue ")
                elif is_it_right == "<":
                    if machine_guess < your_num:
                        lowest = machine_guess + 1
                        counter += 1 
                    else:
                        print("!!!Don't Cheat!!!")
                        your_number = input("What was your number?: ")
                        print(str(machine_guess) +" > " + str(your_number) + ",so you should have written '>' instead of what you wrote.Continue ")
                elif is_it_right == "=": 
                    if machine_guess == your_num:
                        if your_num == machine_guess:
                            counter += 1
                            print("Yayy,I found it")
                            print("It took me " + str(counter) + " tries to guess the number")
                        else:
                            print("You cheated and changed the number during the game.Please play fairly")
                            your_number = input("What was your number?: ")
                            print(str(machine_guess) +" = " + str(your_number) + ",so you should have written '=' instead of what you wrote ")
                        break
                elif is_it_right == 0:
                    you_sure = input("Are you sure you want to leave the game? yes or no: ")
                    if you_sure == "yes":
                        print("Player left the game")
                        break
                machine_guess = (lowest+highest)//2

    elif choice == 0:
        you_sure = input("Are you sure you want to leave the game? yes or no: ")
        if you_sure == "yes":
            print("Player has left the game")
            break

    request = input("Do you want to play again? Answer with 'yes' or 'no': ")
    if request == "no":
        print("You quitted the game")
        break
    elif request == 0:
        you_sure = input("Are you sure you want to leave the game? yes or no: ")
        if you_sure == "yes":
            print("Player left the game")
            break
        

This is my code for game "guess my number",here the complicated ones is me trying to make the program prevent user from cheating (It is a university task,due in 3 hours) So choice 1 is when user decides to play game "guess my number" and 2nd choice when computer plays the game.The problem that I have is :

  1. I can't make the code make the user input the number in range of(1,9999) and THEN continue the process

  2. As you see I have a lot of "if ... == 0" --> .In task it is said that whenever(any of inputs) user types 0 the game has to stop.The others work well but the one in choice 2 the first if is not working

If somebody has solution for this,please help.I would be grateful

Aucun commentaire:

Enregistrer un commentaire