繁体   English   中英

我怎样才能让我的高/低游戏在最后一个结束时提供一个新人? Python

[英]How can I get my Higher/Lower game to offer a new person at the end of the last? Python

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

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

你的代码结构看起来有点奇怪。 通常,“主要” function 被称为一次,并且在 function 内部可以有一个循环。 在文件的顶部,定义在执行期间不会更改的变量。 在循环外的主要 function 中,定义应在循环迭代之间维护的变量。 在循环内部,根据需要修改内容。

我假设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()

另外,请注意,我的示例中的循环会不断从字典中删除元素,直到您用尽选择,或者决定以其他方式结束游戏。 我想这就是你要的?

试试这个,我已经修改了你的代码结构,以便在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()

示例 output(使用我自己制作的数据):

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

暂无
暂无

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

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