繁体   English   中英

如何简化我的基本 python 编码更高/更低的游戏?

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

我是 Python 的初学者,我想根据我学到的东西尝试我自己的小项目。 这是一个更高/更低的游戏,用户获得一个起始数字,然后必须猜测下一个数字是高于还是低于前一个数字。 范围仅在 1-10 之间,对于每个连续的正确猜测,用户得分。

代码在技术上是可行的,我很高兴,但是我相信它很混乱,可以用更简单的方式完成。 由于我必须使用全局变量,因此代码在用户正确回答并继续下一个问题的地方非常笨拙。 如果有人花时间查看它并了解如何简化它,我将不胜感激。 谢谢:)

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 again\n")
        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 again\n")
            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}.")

正如 Timus 所说,代码审查更适合这类问题。 无论如何,这是我的版本。

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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM