Issue
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)
Can anyone help to point me in the right direction here, the while True loop doesn't break when I want it to. giving me the same name as I put in, I would like the loop to repeat as many times until the names are not the same, working with two or more players;
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
As you can see, some of the time the program does not work as expected and I cannot seem to find a workaround to this problem, any help would be greatly appreciated!
Solution
Inside your while loop, you are comparing two variable that don't change. And they are even a different type.
You probably meant to compare your random name with your own:
while True:
import random
random_name = random.choice(recorded_players)
if random_name != your_name:
break
Answered By - gre_gor
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.