简单的Python-如何在for循环的每次重复中更改变量?

问题描述

我的代码用于骰子游戏。它有五轮,每轮结束时加总。我想找到一种方法,可以在棋手结束时将所有五个回合总数加在一起。我在想,最简单的方法是在每个循环的末尾更改回合总计变量的名称。 roundtotal1,roundtotal2等。但是我不确定,我想看看您的想法。谢谢:)

我的代码

Notification.Builder builder = new Notification.Builder(context).setContentTitle(notificationMessage.getTitle())
                    .setContentText(notificationMessage.getVersionMsg())
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
                    .setStyle(new Notification.BigTextStyle().bigText(notificationMessage.getVersionMsg()));

解决方法

import random
import time

p1 = ('Bob')
p2 = ('Larry')
total = 0

#PLAYER 1'S TURN

print('Welcome to Dice Game,',p1)

#Prints round number of 5 rounds
for x in range (1,6):
    round = ('%s' % x)
    print('Round %s' % x)
    time.sleep(0.5)
    print()

#Rolls the first die
    input('Press ENTER to roll your first die...')
    num1 = random.randint(1,6)
    time.sleep(0.5)
    print('...')
    time.sleep(1)
    print('You scored a',num1,'on your first roll!\n')

#Rolls the second die
    time.sleep(2)
    input('Press ENTER to roll your second die...')
    num2 = random.randint(1,num2,'on your second roll!\n')

#If dice rolls 1 and 2 are the same,roll a third die
    time.sleep(1)
    num3 = 0
    if num1 == num2:
        num3 = random.randint(1,6)
        print('As you rolled a double,you get a third die!')
        input('Press enter to roll your third die...')
        time.sleep(0.5)
        print('...')
        time.sleep(1)
        print('You scored a',num3,'on your third roll!\n')
    else:
        pass

#Print the total for the round
    time.sleep(1)
    roundtotal = num1+num2+num3
    print('ROUND TOTAL:',roundtotal)
    print()
    print()
    total = total + roundtotal

print('GAME TOTAL:',total)

必须在顶部声明一个变量,并且始终将其添加到该变量中,而不是在末尾打印该变量

,

最简单的方法是设置另一个变量,该变量仅添加您要滚动的内容,因此将在代码的开始位置total1 = 0定义num1 = random.randint(1,6),然后在行total1 += num1之后添加sqoop import-all-tables --connect jdbc:oracle:thin:server:port:database --username x --password y --warehouse-dir warehouse-dir --hive-import --create-hive-table 将您的总卷数存储在该变量中,以便稍后使用。

如果要将球员拥有的所有掷骰数单独存储在一个变量中,则可以使用列表的概念