我正在尝试创建一个计算降雨量的程序,我在迭代列表以打印一周的日子时遇到了问题。
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
for day in days:
input ("enter the the amount of rainfall (in mm) for", day "week 1")
我知道这是非常错误的,但我被困住了。
答案 0 :(得分:1)
您应该使用,
:
+
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
for day in days:
input ("enter the the amount of rainfall (in mm) for " + day + " week 1")
然后系统会提示您输入值,然后您可以从那里继续:)
答案 1 :(得分:0)
使用字符串连接:
for day in days:
input ("enter the the amount of rainfall (in mm) for " + day + " week 1")
它会创建一个包含所有部分的新字符串。
答案 2 :(得分:0)
您可以尝试使用format,它允许您在python中连接任何类型。
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
register = {}
for day in days:
amount = input ("enter the the amount of rainfall (in mm) for {0} week 1".format(day))
register[day] = amount
for day in register:
print '{0} rainfall : {1}'.format(day, register[day])
答案 3 :(得分:0)
根据@ Mathias711,input
方法需要一个字符串,因此您无法传递逗号分隔列表(您可以使用print
方法)。
如果要跟踪输入的总数,则需要将输入添加到变量中。因为看起来你期望一个整数值,你应该将输入从字符串转换为int。 E.g。
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
total = 0
for day in days:
total += int(input ("enter the the amount of rainfall (in mm) for " + day + " week 1"))