number_of_players = int(input("Please input the number of players playing: "))
recorded_players = []
for counter in range(0, number_of_players):
name_of_friend = str(input("Input players first name: "))
recorded_players.append(name_of_friend)
your_name = str(input("Please enter your name: "))
while True:
import random
random_name = random.choice(recorded_players)
if recorded_players != your_name:
break
friend_name = (random_name.upper())
selected_name = (your_name.upper())
gift_price = (len(friend_name)) * 2
print(selected_name,"You have bought a gift for your friend", friend_name, "which costs £", gift_price)
任何人都可以帮助我指出正确的方向,而真正的循环并没有在我想要的时候打破。给我一个与我输入相同的名字,我希望循环重复多次,直到名字不相同,与两个或更多玩家合作;
Please input the number of players playing: 2
Input players first name: Bob
Input players first name: Alice
Please enter your name: Bob
BOB You have bought a gift for your friend BOB which costs £ 6
正如您所看到的,有些时候程序没有按预期工作,我似乎无法找到解决此问题的方法,任何帮助都将不胜感激!
答案 0 :(得分:0)
在while循环中,您要比较两个不会改变的变量。它们甚至是一种不同的类型。
您可能想要将您的随机名称与您自己的名称进行比较:
while True:
import random
random_name = random.choice(recorded_players)
if random_name != your_name:
break