如何在Python中将变量值设置为另一个变量?

时间:2016-01-19 19:58:09

标签: python python-3.x

基本上,我有一个关于板球的问题,并让用户通过使用while循环为6个碗中的每一个输入他们的分数,然后在结束时给他们总计结束。这是我已经完成的代码,但它不起作用,而且我不确定如何使其正确。

    cricket_balls=0
    while cricket_balls < 6:
        score= int(input('What was the score of bowl', cricket_balls , '?:' )
        cricket_balls = cricket_balls + 1
        print (score)
    total_score = score
    print ('The total score for this over is', total_score)

2 个答案:

答案 0 :(得分:0)

有三个问题。你忘了为分数写一个计数器,一个右括号和input()只占一个位置参数。你的代码应该是:

cricket_balls, total_score = 0, 0
while cricket_balls < 6:
    score = int(input('What was the score of bowl {} ?'.format(cricket_balls)))
    cricket_balls += 1
    total_score += score
    print(score)
total_score = score
print('The total score for this over is', total_score)

答案 1 :(得分:0)

input接受一个参数,因此您需要连接组件字符串以创建单个字符串。有几种选择:

score = int(input('What was the score of bowl %d ?:' % (cricket_balls,)))
score = int(input('What was the score of bowl ' + str(cricket_balls) + '?:'))
score = int(input('What was the score of bowl {} ?:'.format(cricket_balls)))