from random import randint
# -------------------------
# Subprograms
# -------------------------
# Gets user input and directs it to the converter
def get_player_choose():
global choose
choose = input("Enter your choice, R,P,S")
if choose.upper() == "R" or choose.upper() == "S" or choose.upper() == "P":
choose = choose.lower()
conver(choose)
else:
exit()
# converts the input to lower case and outputs full name
# (NOTE conver(choose) - i was getting an error and i had to change all my variable names)
def conver(choose):
if choose == "r":
print("rock")
elif choose == "p":
print("paper")
else:
if choose == "s":
print("scissors")
cpu_chose()
# cpu's choice (pretty self explanitory)
def cpu_chose():
num = randint(1, 4)
if num == 1:
cpu_choose = "r"
elif num == 2:
cpu_choose = "p"
else:
cpu_choose = "s"
print(cpu_choose, "cpu") # test cases to see where program fails etc
who_won_round(cpu_choose, choose)
def who_won_round(cpu_choose, choose):
# FIXED issue with the fact the cpu choice doesnt change so same output everytime maybe use num instead of cpu_choose variable
counter = 0
counter1 = 0
print(counter, choose, cpu_choose)
counter = 0
counter1 = 0
counterlist = []
while counter != 5 or counter1 != 5:
# **COUNTER not updating, in the loop it updates once and stays at 1**
if counter == 5 or counter1 == 5:
exit()
elif cpu_choose == "r" and choose == "p":
print("Human wins!")
counterlist.append(
counter + 1
) # just tried this as a last ditch effort didnt work :'(
print("test1", counterlist)
elif cpu_choose == "s" and choose == "r":
print("Human wins!")
counter = (str(counter), "1")
print("test2", counter)
elif cpu_choose == "p" and choose == "r":
print("Human wins!")
counter = (str(counter), "1")
print("test3", counter)
elif cpu_choose == choose:
print("Its a draw")
counter = (str(counter), "1")
counter1 = (str(counter1), "1")
print("test4", counter1)
else:
print("AI wins!")
counter1 = (str(counter1), "1")
print("test5", counter1)
cpu_choose = cpu_chose()
print("HAHSDAHSDA COUNTER", counter) # test case which didnt print
print(counter, "human")
print(counter1, "ai")
# This seems to work when the other code works
if counter1 > counter:
print("Ai wins, better luck next time!")
elif counter > counter1:
print("You win, good job!")
else:
print("error")
# IGNORE
"""def count(counter,counter1):
counter = counter+1
def count1(counter,counter1):
counter1 = counter1+1"""
def play_game():
get_player_choose()
# -------------------------
# Main program
# ------------------------
play_game()
顺便说一句,很抱歉代码转储,但我认为您需要所有代码才能使其有意义
我只是一名学生,所以不要把我骂得太厉害,但我原以为每次人类/人工智能获胜时计数器就会增加一,但事实并非如此。
我的问题是子程序中while循环中的counter和counter1,who_won_round()只更新一次,无限循环,就好像counter每次都是0一样。因此,非常感谢任何帮助,我真的不确定问题是什么,而且我在任何地方都找不到解决方案? 任何帮助将不胜感激。 是的,任务要求每一位代码都位于子程序中
请查看更正后的代码:
from random import randint
# No need for global variables here. It's better to pass them around functions.
# This function gets the player's choice and validates it
def get_player_choice():
choice = input("Enter your choice (R, P, S): ").upper()
if choice in ("R", "S", "P"):
return choice.lower() # Returning the value is better than using a global variable
else:
print("Invalid choice. Exiting the game.")
exit() # If the choice is invalid, exit the program
# This function converts the input to the full name
def convert(choice):
choices = {"r": "rock", "p": "paper", "s": "scissors"}
return choices.get(choice, "Invalid choice")
# This function generates the CPU's choice
def cpu_choose():
num = randint(1, 3) # The range should be 1, 3 because there are only 3 options
choices = {1: "r", 2: "p", 3: "s"}
cpu_choice = choices[num]
print(f"{convert(cpu_choice)} cpu") # Convert to full name before printing
return cpu_choice
# This function determines who won the round
def who_won_round(cpu_choice, player_choice, player_counter, cpu_counter):
win_conditions = {('p', 'r'): "Human wins!", ('r', 's'): "Human wins!", ('s', 'p'): "Human wins!"}
lose_conditions = {('r', 'p'): "AI wins!", ('s', 'r'): "AI wins!", ('p', 's'): "AI wins!"}
print(f"Player chose {convert(player_choice)}, CPU chose {convert(cpu_choice)}.")
if player_choice == cpu_choice:
print("It's a draw")
elif (player_choice, cpu_choice) in win_conditions:
print(win_conditions[(player_choice, cpu_choice)])
player_counter += 1
elif (player_choice, cpu_choice) in lose_conditions:
print(lose_conditions[(player_choice, cpu_choice)])
cpu_counter += 1
return player_counter, cpu_counter
# Main function to play the game
def play_game():
player_counter = 0
cpu_counter = 0
while player_counter < 5 and cpu_counter < 5:
player_choice = get_player_choice()
cpu_choice = cpu_choose()
player_counter, cpu_counter = who_won_round(cpu_choice, player_choice, player_counter, cpu_counter)
print(f"Current Score - Human: {player_counter} | AI: {cpu_counter}")
if player_counter > cpu_counter:
print("You win, good job!")
elif cpu_counter > player_counter:
print("AI wins, better luck next time!")
else:
print("It's a draw overall.")
play_game()
更正:
randint
范围。在 randint()
中包含了两个选项,因此如果使用 (1, 4) 它将比其他选项给出更多的 's'。祝你好运!