Python 编写一个程序,要求用户输入一周中每天在食品和天然气上花费的金额
我的任务是编写一个程序,要求用户输入一周中每天在食物和汽油上花费的金额。计算并显示用户每天在食品上花费超过20美元,在汽油上花费超过10美元的天数。我的问题是,我不知道如何让输出显示“你在一周中的几天里每天花在食物上的钱超过20美元。”和“你在一周中的几天里每天花在汽油上的钱超过10美元。”因为我必须在引号之间输入答案,要么显示语法错误,要么将其放在新行上。我的代码是:Python 编写一个程序,要求用户输入一周中每天在食品和天然气上花费的金额,python,Python,我的任务是编写一个程序,要求用户输入一周中每天在食物和汽油上花费的金额。计算并显示用户每天在食品上花费超过20美元,在汽油上花费超过10美元的天数。我的问题是,我不知道如何让输出显示“你在一周中的几天里每天花在食物上的钱超过20美元。”和“你在一周中的几天里每天花在汽油上的钱超过10美元。”因为我必须在引号之间输入答案,要么显示语法错误,要么将其放在新行上。我的代码是: over20food=0; over10gas=0; food=input("Please enter the amount
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));
你的尝试几乎是正确的。您只是在第一行中缺少了一个
+
。我下面的回答假设您的其余代码工作正常,变量20food
和10gas
是正确的
另外,正如John Gordon所指出的,您应该将int()
作为int(input())
命令放置在input()的周围
input()
返回字符串。如果要将输入读取为数值,则需要对结果调用int()
,如下所示:
gas = int(input("Please enter the amount spent on gas on Sunday:"))
您的print()
是错误的,因为变量名后面和消息的其余部分之间没有+
另外,去掉那些分号。Python不需要它们。看起来您在第一个print语句中缺少了一个“+”
应该是
print("You spent more than 20 dollars per day on food in" +str(over20food) + "days of the week")
你们学过列表了吗?您可以简化它,在Python中,行的末尾不需要分号。@vash_the_stampede不,我们还没有学过lists@wwii是的,我现在知道了。我只是忘了,因为我先学了C#。谢谢,但是你知道如何在“in”之后留出一个空格吗?这并不重要,但我提交的网站将我的作业标记为错误,因为我在“in”之后没有空格。请参见下面Bazingaa对其答案的评论Hanks,但你知道如何在“in”之后留出空格吗?这并不重要,但我提交的网站将我的作业标记为错误,因为我在“in”之后没有空格。只需在字符串中加一个空格,如“你每天花在in的汽油上的钱超过10美元”
print("You spent more than 20 dollars per day on food in" +str(over20food) + "days of the week")