循环循环多次,然后添加



我正在编写一个基于for循环的程序。我有一个基于随机导入的幸运数字。当幸运数字加起来等于偶数时,我想把数字重新滚动7次,用1乘1打印出来,最后把它们都加起来。我一直在尝试,但我对使用for循环感到困惑。感谢提前提供的帮助

import random
lucky_1: random.randint(1,56)
lucky_2 : random.randint(1, 56)
while (lucky_1 + lucky_2 % 2) == 0:
print("You're even!")
for (lucky_1) in range (1, 7):
print (lucky_1)
choice = input("Would you like to see your numbers:")
if choice == 'yes':
print (lucky_1 + lucky_1)

我不确定我是否能正确理解你的问题。但是,试试这样的东西:

import random
lucky_1 = random.randint(1,56)
lucky_2 = random.randint(1, 56)
def lucky(lucky_1, lucky_2):
numbers = []
if (lucky_1 + lucky_2) % 2 == 0:
print("You're Even!nRe-rolling the numbers...nn")
for i in range(1, 8):
lucky_1 = random.randint(1, 56)
lucky_2 = random.randint(1, 56)
numbers.append(lucky_1+lucky_2)
print(f'Roll: {i}')
print(f'Your lucky number 1 -> {lucky_1}')
print(f'Your lucky number 2 -> {lucky_2}n')
choice = input("Would you like to see your numbers: ")
if choice == 'yes':
print(f'n=====================================nYour final lucky numbers are {lucky_1} and {lucky_2}')
print(f'Sum of numbers from seven rolls = {sum(numbers)}n=====================================')
else:
print('Thank you.')
lucky(lucky_1, lucky_1)

结果

You're Even!
Re-rolling the numbers...

Roll: 1
Your lucky number 1 -> 52
Your lucky number 2 -> 23
Roll: 2
Your lucky number 1 -> 4
Your lucky number 2 -> 43
Roll: 3
Your lucky number 1 -> 29
Your lucky number 2 -> 23
Roll: 4
Your lucky number 1 -> 47
Your lucky number 2 -> 11
Roll: 5
Your lucky number 1 -> 24
Your lucky number 2 -> 50
Roll: 6
Your lucky number 1 -> 17
Your lucky number 2 -> 31
Roll: 7
Your lucky number 1 -> 51
Your lucky number 2 -> 1
Would you like to see your numbers: yes
=====================================
Your final lucky numbers are 51 and 1
Sum of numbers from seven rolls = 406
=====================================

最新更新