使用 if elif else 语句 + 导入随机选择创建了 python 彩票抽奖
Created a python lottery draw using if elif else statements + import random choice
我尝试通过使用导入随机选择以及 if、elif 和 else 语句在 python 中创建彩票游戏。我想它正在正常工作。我怎样才能使用一个函数来复制这个彩票游戏?谢谢
from random import choice # import module
# I created a list of lottery numbers below:
lottery_numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'a', 'b', 'c', 'd', 'e']
# the person who gets this combinations of numbers and letters on the ticket wins
print("if the following numbers or letters are chosen \n"
"the person wins a prize: e, 1, b, 10 ")
if (choice(lottery_numbers)) == 'e':
print("winner!!")
elif (choice(lottery_numbers)) == '1':
print('winner!')
elif (choice(lottery_numbers)) == 'b':
print("winner")
elif (choice(lottery_numbers)) == '10':
print("winner")
else:
print('you lose, next time!!')
count = 0
#this does the choice function four times and checks if the number variable is equal to one of the lottery values. If so, it increments count by 1
for i in range(4):
number = choice(lottery_numbers)
if number == 'e' or number == '1' or number == 'b' or number == '10':
count+=1
# count must = four because there are four lottery numbers
if count == 4:
print('you won')
else:
print('you lose')
我尝试通过使用导入随机选择以及 if、elif 和 else 语句在 python 中创建彩票游戏。我想它正在正常工作。我怎样才能使用一个函数来复制这个彩票游戏?谢谢
from random import choice # import module
# I created a list of lottery numbers below:
lottery_numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'a', 'b', 'c', 'd', 'e']
# the person who gets this combinations of numbers and letters on the ticket wins
print("if the following numbers or letters are chosen \n"
"the person wins a prize: e, 1, b, 10 ")
if (choice(lottery_numbers)) == 'e':
print("winner!!")
elif (choice(lottery_numbers)) == '1':
print('winner!')
elif (choice(lottery_numbers)) == 'b':
print("winner")
elif (choice(lottery_numbers)) == '10':
print("winner")
else:
print('you lose, next time!!')
count = 0
#this does the choice function four times and checks if the number variable is equal to one of the lottery values. If so, it increments count by 1
for i in range(4):
number = choice(lottery_numbers)
if number == 'e' or number == '1' or number == 'b' or number == '10':
count+=1
# count must = four because there are four lottery numbers
if count == 4:
print('you won')
else:
print('you lose')