簡體   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