用python写一个简单的彩票
时间: 2024-05-29 19:05:41 浏览: 130
基于python写的一个简易彩票随机工具
当然可以!下面是一个用Python编写的简单彩票程序的示例:
```python
import random
def generate_lottery_numbers():
numbers = random.sample(range(1, 51), 6)
return sorted(numbers)
def get_user_numbers():
user_numbers = []
for i in range(6):
while True:
try:
number = int(input("请输入第{}个数字(1-50):".format(i+1)))
if 1 <= number <= 50:
break
else:
print("请输入1-50之间的数字!")
except ValueError:
print("请输入有效的数字!")
user_numbers.append(number)
return sorted(user_numbers)
def check_results(user_numbers, lottery_numbers):
matched_numbers = set(user_numbers) & set(lottery_numbers)
num_matched = len(matched_numbers)
print("您选择的号码是:", user_numbers)
print("中奖号码是:", lottery_numbers)
print("您猜中了{}个号码!".format(num_matched))
if num_matched == 6:
print("恭喜您中了一等奖!")
elif num_matched == 5:
print("恭喜您中了二等奖!")
elif num_matched == 4:
print("恭喜您中了三等奖!")
else:
print("很遗憾,您没有中奖。")
# 生成彩票号码
lottery_numbers = generate_lottery_numbers()
# 获取用户选择的号码
user_numbers = get_user_numbers()
# 检查结果并输出中奖信息
check_results(user_numbers, lottery_numbers)
```
这个程序会生成一组随机的彩票号码,然后要求用户输入自己选择的号码,最后会比较用户选择的号码和中奖号码,并输出中奖信息。
阅读全文