How to simplify my basic python coding higher/lower game?

Question:

I’m a beginner at Python and I wanted to try out my own little project based off the things I’ve learnt. It’s a higher/lower game where the user gets a starting number and then has to guess if the next number is higher or lower than the previous one. The range is only between 1-10 inclusive and for each consecutive correct guess, the user scores a point.

The code technically works which I’m pleased about, however I believe it’s quite messy and could be done in a far simpler way. The code is quite clunky around where the user gets their answer right and moves on to the next question as I had to use global variables. I would highly appreciate if someone took the time to look over it and see how it could get simplified. Thank you 🙂

import random

number_list = [1,2,3,4,5,6,7,8,9,10]
start_number = random.choice(number_list)
should_continue = True
move_on = False
end_game = False
score = 0


def deal():
    return random.choice(number_list)
def higher_lower():
    input("Is the next number 'higher' or 'lower': ")
    return
def question():
    global should_continue, move_on, score, end_game, choice
    while should_continue:      
        choice = deal()
        if next_answer == "higher" and choice > old_number:
            print(f"The new number is {choice}. Good job!n")
            score += 1
            should_continue = False
            move_on = True
        elif next_answer == "lower" and choice < old_number:
            print(f"The new number is {choice}. Good job!n")
            score += 1
            should_continue = False
            move_on = True
        elif choice == old_number:
            print("It's a draw. Please choose againn")
        else:
            print(f"The new number is {choice}. You lose!n")
            should_continue = False
            move_on = True
            end_game = True



print("Welcome to the Higher/Lower game!n")
print("The rules of the game are simple.")
print("You start off with a number, and you have to guess if the next number is 'higher' or 'lower'.")
print("You will need to reply back with either 'higher' or lower'.")
print("The numbers are between 1-10 inclusive. For each correct answer, you get a point.")
print("The goal is to get as many points as possible without getting a wrong answer. Good luck!n")

while not end_game:
    while not move_on:
        while should_continue:
            print(f"Your first number is {start_number}.")
            first_answer = input("Is the next number 'higher' or 'lower': ")
            choice = deal()
            if first_answer == "higher" and choice > start_number:
                print(f"The new number is {choice}. Good job!n")
                score += 1
                should_continue = False
                move_on = True
            elif first_answer == "lower" and choice < start_number:
                print(f"The new number is {choice}. Good job!n")
                score += 1
                should_continue = False
                move_on = True
            elif choice == start_number:
                print("It's a draw. Please choose againn")
            else:
                print(f"The new number is {choice}. You lose!n")
                should_continue = False
                move_on = True
                end_game = True
    move_on = False
    while not move_on and not end_game:
        old_number = choice
        next_answer = input("Is the next number 'higher' or 'lower': ")
        should_continue = True
        question()
    move_on = False
    while not move_on and not end_game:
        old_number = choice
        next_answer = input("Is the next number 'higher' or 'lower': ")
        should_continue = True
        question()
    move_on = False
    while not move_on and not end_game:
        old_number = choice
        next_answer = input("Is the next number 'higher' or 'lower': ")
        should_continue = True
        question()
    move_on = False
    while not move_on and not end_game:
        old_number = choice
        next_answer = input("Is the next number 'higher' or 'lower': ")
        should_continue = True
        question()
        
print(f"Your final score is {score}.")

Asked By: beginnercoder2021

||

Answers:

As Timus said, code review is better for this kind of question. Anyway, here’s my version.

import random

c_list = {'higher': 1 , 'lower': 0}

def game():
    score = 0
    cur_num = random.randint(1,10)
    old_num = cur_num
    print("Your starting number is " + str(cur_num))
    
    while True:
        while cur_num == old_num:
            cur_num = random.randint(1,10)
        choice = input("Is the next number 'higher' or 'lower': ")
        if (cur_num > old_num) == c_list[choice]:
            print("Good job! The new number is " + str(cur_num))
            score += 1
            old_num = cur_num
        else:
            print("you lose! New number is " + str(cur_num))
            break
    
    return score

print("Welcome to the Higher/Lower game!n")
print("The rules of the game are simple.")
print("You start off with a number, and you have to guess if the next number is 'higher' or 'lower'.")
print("You will need to reply back with either 'higher' or lower'.")
print("The numbers are between 1-10 inclusive. For each correct answer, you get a point.")
print("The goal is to get as many points as possible without getting a wrong answer. Good luck!n")

high_score = 0

while True:
    cur_score = game()
    if cur_score > high_score:
        high_score = cur_score
    print("Your score is " + str(cur_score))
    inp = "o"
    while inp not in ['Y', 'y', 'N', 'n']:
        inp = input("Want to continue? (Y/N): ")
    if inp in ['N', 'n']:
        break
    else:
        print("Start of new game.")

print("End of game. Your highest score is " + str(high_score))
Answered By: Abdur Rakib