编写一个程序,要求用户输入一周中每一天在食品和天然气上花费的金额

时间:2018-09-23 22:53:41

标签: python

我的任务是编写一个程序,要求用户输入一周中每一天在食品和天然气上花费的金额。计算并显示用户每天在食物上花费超过20美元,在天然气上花费超过10美元/天的天数。我的问题是我不知道如何使输出说:“您在一周的_天内每天花在食物上的钱超过20美元。”而且,“一周之内,您每天在汽油上的花费超过10美元。”因为我必须在引号之间输入答案,要么说语法错误,要么将其放在新行中。我的代码是:

over20food=0;
over10gas=0;
food=input("Please enter the amount spent on food on Monday:");
if (food>20):
    over20food = over20food + 1;
gas=input("Please enter the amount spent on gas on Monday:");
if (gas>10):
    over10gas = over10gas + 1;
food=input("Please enter the amount spent on food on Tuesday:");
if (food>20):
    over20food = over20food + 1;
gas=input("Please enter the amount spent on gas on Tuesday:");
if (gas>10):
    over10gas = over10gas + 1;
food=input("Please enter the amount spent on food on Wednesday:");
if (food>20):
    over20food = over20food + 1;
gas=input("Please enter the amount spent on gas on Wednesday:");
if (gas>10):
    over10gas = over10gas + 1;
food=input("Please enter the amount spent on food on Thursday:");
if (food>20):
    over20food = over20food + 1;
gas=input("Please enter the amount spent on gas on Thursday:");
if (gas>10):
    over10gas = over10gas + 1;
food=input("Please enter the amount spent on food on Friday:");
if (food>20):
    over20food = over20food + 1;
gas=input("Please enter the amount spent on gas on Friday:");
if (gas>10):
    over10gas = over10gas + 1;
food=input("Please enter the amount spent on food on Saturday:");
if (food>20):
    over20food = over20food + 1;
gas=input("Please enter the amount spent on gas on Saturday:");
if (gas>10):
    over10gas = over10gas + 1;
food=input("Please enter the amount spent on food on Sunday:");
if (food>20):
    over20food = over20food + 1;
gas=input("Please enter the amount spent on gas on Sunday:");
if (gas>10):
    over10gas = over10gas + 1;
print("You spent more than 20 dollars per day on food in" +str(over20food) "days of the week");
print("You spent more than 10 dollars per day on gas in" +str (over10gas));

3 个答案:

答案 0 :(得分:1)

您似乎在第一个打印语句中缺少“ +”。

应该是

print("You spent more than 20 dollars per day on food in" +str(over20food) + "days of the week")

答案 1 :(得分:0)

您的尝试几乎是正确的。您仅在第一行中缺少over20food。我在下面的回答中假设您的其余代码工作正常,并且变量over10gasint()是正确的。

此外,正如约翰·戈登(John Gordon)所指出的那样,您应将input()命令的 ALL 周围的int(input())设置为print("You spent more than 20 dollars per day on food in" + str(over20food) + "days of the week"); print("You spent more than 10 dollars per day on gas in" + str(over10gas) + "days of the week");

Winston

答案 2 :(得分:0)

int()返回字符串。如果要将输入读取为数值,则需要对结果调用gas = int(input("Please enter the amount spent on gas on Sunday:")) ,如下所示:

print()

您的+是错误的,因为在变量名和消息的其余部分之间没有$howMuch = '17шт / 119ml / 255грн'; $blocks = explode('/',$howMuch); echo $blocks[2];

此外,摆脱那些分号。 Python不需要它们。