如何修改代码中的+ add,以便打印获胜者的姓名和中奖号码?!因此,让我们说威廉的随机数7与中奖号码7相匹配。然后我希望它打印出来#34;威廉是胜利者,中奖号码是7!"。另一方面,如果中奖号码为5且没有人拥有匹配的号码,我希望它打印出来#34;没有赢家"。提前谢谢!
import random
from random import sample
character_count = 5
names = ['John', 'William', 'James', 'George', 'Charles', 'Frank', 'Joseph', 'Henry', 'Robert', 'Thomas', 'Edward',
'Harry', 'Walter', 'Arthur', 'Fred', 'Albert', 'Samuel', 'Clarence', 'Louis', 'David', 'Mary', 'Anna', 'Emma', 'Elizabeth',
'Margaret', 'Minnie', 'Ida', 'Bertha', 'Clara', 'Alice', 'Annie', 'Florence', 'Bessie', 'Grace', 'Ethel', 'Sarah',
'Ella', 'Martha', 'Nellie', 'Mabel']
generate_random_names = (sample(names, k=character_count))
random_number = random.sample(range(1,10), (5))
winning_number = random.sample(range(1,10), (1))
character_status = list(zip(generate_random_names, random_number))
char_one_info = (character_status[0])
char_two_info = (character_status[1])
char_three_info = (character_status[2])
char_four_info = (character_status[3])
char_five_info = (character_status[4])
while True:
winning_number == char_one_info[1] or winning_number == char_two_info[1] or winning_number == char_three_info[1] or winning_number == char_four_info[1] or winning_number == char_five_info[1]
print (True)
答案 0 :(得分:2)
稍微编辑您的代码,使其更具可读性和简化的导入。
import random
character_count = 5
names = ['John', 'William', 'James', 'George', 'Charles', 'Frank', 'Joseph', 'Henry', 'Robert', 'Thomas', 'Edward’, 'Harry', 'Walter', 'Arthur', 'Fred', 'Albert', 'Samuel', 'Clarence', 'Louis', 'David', 'Mary', 'Anna', 'Emma', 'Elizabeth’, 'Margaret', 'Minnie', 'Ida', 'Bertha', 'Clara', 'Alice', 'Annie', 'Florence', 'Bessie', 'Grace', 'Ethel', 'Sarah’, 'Ella', 'Martha', 'Nellie', 'Mabel']
generate_random_names = random.sample(names, k=character_count)
random_numbers = random.sample(range(1,10), 5)
winning_number = random.randint(1, 10)
character_status = list(zip(generate_random_names, random_numbers))
if winning_number in random_numbers:
winner = [c for c in character_status if c[1] == winning_number][0]
print(f"{winner[0]} is the winner and the winner had {winner[1]}!")
else:
print("Nobody won")
请注意,如果您正在运行< 3.6,请将print(f"{winner[0]} is the winner and the winner had {winner[1]}!”)
更改为print(“{name} is the winner and the winner had {num}!”.format(name=winner[0], num=winner[1])
答案 1 :(得分:2)
我不确定循环是否应该继续,直到找到胜利者,所以我已将其删除。我使用dict
存储数字和名称之间的映射,这样我就可以轻松查找是否有人赢了(彩票?)。您没有指定python版本,但下面的代码适用于2.7。
import random
character_count = 5
names = ['John', 'William', 'James', 'George', 'Charles', 'Frank', 'Joseph',
'Henry', 'Robert', 'Thomas', 'Edward', 'Harry', 'Walter', 'Arthur',
'Fred', 'Albert', 'Samuel', 'Clarence', 'Louis', 'David', 'Mary',
'Anna', 'Emma', 'Elizabeth', 'Margaret', 'Minnie', 'Ida', 'Bertha',
'Clara', 'Alice', 'Annie', 'Florence', 'Bessie', 'Grace', 'Ethel',
'Sarah', 'Ella', 'Martha', 'Nellie',
'Mabel']
generate_random_names = (random.sample(names, k=character_count))
random_number = random.sample(range(1, 10), 5)
character_status = list(zip(random_number, generate_random_names))
char_info = dict([character_status[i] for i in range(5)])
# You can just use randint here
winning_number = random.randint(1, 10)
while True:
if winning_number in char_info:
name = char_info[winning_number]
print "{0} is the winner and the winning number was {1}!"\
.format(name, winning_number)
else:
print "There is no winner"
break