如何让我的Higher/Lower游戏在最后一场比赛结束时提供一个新人



我正在努力做到这一点,这样,在问用户一个问题后,他们认为哪个答案更高,我的程序将确定答案是正确的还是不正确的,如果答案正确,它将为他们提供他们刚刚猜对的那个人,然后从数据列表中为他们提供一个新的随机人。(不确定这是否有意义,但我希望有人能帮我,因为我现在很困!(

import random
from Games.Day9.game_data import data
from Games.Day9.art import logo
from Games.Day9.art import vs
# loop:
game_end = False
# score
score = 0
# random person 1:
random_person1 = random.choice(data)
# random person 2:
random_person2 = random.choice(data)
# making sure the player doesn't get two of the same people:
if random_person2 == random_person1:
random_person2 = random.choice(data)

"""Takes the account data and return a printable format of code."""
# formatting 1:
account_name1 = random_person1["name"]
account_followers1 = random_person1["follower_count"]  # remove these from printer at first
account_description1 = random_person1["description"]
account_country1 = random_person1["country"]
# formatting2:
account_name2 = random_person2["name"]
account_followers2 = random_person2["follower_count"]
account_description2 = random_person2["description"]
account_country2 = random_person2["country"]

def start():
# higher or lower logo:
print(logo)
# where the first option goes:
print(f"Compare A: {account_name1}, a {account_description1}, from {account_country1}")
# vs sign:
print(vs)
# where the second option goes:
print(f"Against B: {account_name2}, a {account_description2}, from {account_country2}")

while not game_end:
def main():
# globals:
global score
global game_end
start()
print(f"Your current score is {score}")
# the users guess:
guess = input("Who has more followers? Type 'A' or 'B':n").upper()
if guess == "A":
if account_followers1 > account_followers2:
score += 1
print(f"You're correct! Your score is: {score}")
elif account_followers1 < account_followers2:
print(f"Sorry you were wrong. Your final score was {score}")
game_end = True
elif guess == "B":
if account_followers2 > account_followers1:
score += 1
print(f"You're correct! Your score is: {score}")
elif account_followers2 < account_followers1:
print(f"Sorry you were wrong. Your final score was {score}")
game_end = True

main()

尝试一下,我已经修改了您的代码结构,以便更好地使用while循环。这将持续要求一个不同的独特的人每次。

import random
from Games.Day9.game_data import data
from Games.Day9.art import logo
from Games.Day9.art import vs
# making sure the player doesn't get two of the same people:

def get_random_person(person_1, person_2):
original_person_2 = person_2
while person_1 == person_2 or original_person_2 == person_2:
person_2 = random.choice(data)

return [person_1, person_2]

def start(random_person1, random_person2):
"""Takes the account data and return a printable format of code."""
# formatting 1:
account_name1 = random_person1["name"]
account_followers1 = random_person1["follower_count"]  # remove these from printer at first
account_description1 = random_person1["description"]
account_country1 = random_person1["country"]
# formatting2:
account_name2 = random_person2["name"]
account_followers2 = random_person2["follower_count"]
account_description2 = random_person2["description"]
account_country2 = random_person2["country"]
# higher or lower logo:
print(logo)
# where the first option goes:
print(f"Compare A: {account_name1}, a {account_description1}, from {account_country1}")
# vs sign:
print(vs)
# where the second option goes:
print(f"Against B: {account_name2}, a {account_description2}, from {account_country2}")
return account_followers1, account_followers2

def main():
score = 0
random_person1 = random.choice(data)
random_person2 = None
while True:
random_person1, random_person2 = get_random_person(random_person1, random_person2)
account_followers1, account_followers2 = start(random_person1, random_person2)
print(f"Your current score is {score}")
# the users guess:
guess = input("Who has more followers? Type 'A' or 'B':n").upper()
if guess == "A":
if account_followers1 > account_followers2:
score += 1
print(f"You're correct! Your score is: {score}")
elif account_followers1 < account_followers2:
print(f"Sorry you were wrong. Your final score was {score}")
break
elif guess == "B":
if account_followers2 > account_followers1:
score += 1
print(f"You're correct! Your score is: {score}")
random_person1 = random_person2
elif account_followers2 < account_followers1:
print(f"Sorry you were wrong. Your final score was {score}")
break
main()

示例输出(带有我自己制作的数据(:

Compare A: b, a bar, from gbr
Against B: c, a bat, from aus
Your current score is 0
Who has more followers? Type 'A' or 'B':
B
You're correct! Your score is: 1
Compare A: c, a bat, from aus
Against B: a, a foo, from usa
Your current score is 1
Who has more followers? Type 'A' or 'B':
A
You're correct! Your score is: 2
Compare A: c, a bat, from aus
Against B: b, a bar, from gbr
Your current score is 2
Who has more followers? Type 'A' or 'B':
A
You're correct! Your score is: 3
Compare A: c, a bat, from aus
Against B: a, a foo, from usa
Your current score is 3
Who has more followers? Type 'A' or 'B':
A
You're correct! Your score is: 4
Compare A: c, a bat, from aus
Against B: b, a bar, from gbr
Your current score is 4
Who has more followers? Type 'A' or 'B':
A
You're correct! Your score is: 5
Compare A: c, a bat, from aus
Against B: a, a foo, from usa
Your current score is 5
Who has more followers? Type 'A' or 'B':
A
You're correct! Your score is: 6
Compare A: c, a bat, from aus
Against B: b, a bar, from gbr
Your current score is 6
Who has more followers? Type 'A' or 'B':
A
You're correct! Your score is: 7
Compare A: c, a bat, from aus
Against B: a, a foo, from usa
Your current score is 7
Who has more followers? Type 'A' or 'B':
A
You're correct! Your score is: 8
Compare A: c, a bat, from aus
Against B: b, a bar, from gbr
Your current score is 8
Who has more followers? Type 'A' or 'B':
B
Sorry you were wrong. Your final score was 8

您的代码结构看起来有点奇怪。通常,"main"函数被一次性调用,在该函数内部可以有一个循环。在文件的顶部,定义在执行过程中不会更改的变量。在循环外的主函数中,定义应在循环迭代之间维护的变量。在循环中,根据需要修改内容。

我假设data是一条格言。

例如:

# Imports and Constants
import random
from Games.Day9.game_data import data

def main():
# Game Variables
score = 0
game_end = False
data: dict
while not game_end and len(data) >= 2:
# Pick 2 random elements from the dictionary and remove them
a = data.pop(random.choice(data.keys()))
b = data.pop(random.choice(data.keys()))

...

print("Game Over!")

if __name__ == "__main__":
main()

此外,请注意,在我的例子中,循环会不断从dict中删除元素,直到你没有选择,或者决定以其他方式结束游戏。我想这就是你想要的?

最新更新