我正在通过一本名为“学习python艰难之路3”的书学习Python。
我所做的正是作者所要求的,但是得到了不同的值。下面是问题的总结部分。我现在在路上,所以没有这本书,但这是记忆中的,因为我昨晚尝试了100次。
age = '35'
height = '74'
weight = '180'
total = {age} + {height} + {weight}
print(f"If I add my {age}, {height} and {weight}, I get {total}.")作者说我应该得到289。然而,我一直得到3574180。我重新输入,一遍又一遍地校对,仍然得到了年龄,身高和体重3574180的字符串,而不是这三个字符串的总和。我很困惑,并希望得到任何反馈。非常感谢你提前这么做。
发布于 2019-05-16 22:19:09
您正在添加字符串,因此您的结果是年龄、体重和身高的串联。取而代之的是:
total = int(age) + int(height) + int(weight)这会将这些值转换为int,它们可以相加在一起
发布于 2019-05-16 22:23:11
这里发生的事情是,你输入的年龄、身高和体重是一个字符串'‘,所以总共发生的事情是
total = '35' + '74' + '180' 它们只是组合在一起,这里不是计算
要计算该字符串,必须将其转换为整数或浮点数
total = int(age) + int(height) + int(weight)这将执行数学计算,您的代码将正常工作
发布于 2020-10-07 06:51:47
在处理完逻辑错误之后,我相信正确的程序代码如下所示。我已经测试了几次,它会相应地计算收益。
barsInACase = 12
costPerCase = 8.00
costPerSingleBar = 1.00
#asks for the user to enter the number of bars they sold
sold = int (input ("How many candy bars did you sale? "))
#calculates the number of cases sold based off of the users input
casesSold = sold / barsInACase
#calculates the net earnings using the users input
netEarnings = (sold * costPerSingleBar) - (casesSold * costPerCase)
#displays the net earnings
print ("Your net earnings are $",
format (netEarnings, ',.2f'), ".", sep="")
#calculates the amount that the SGA gets
studentGovernmentAssociation = netEarnings * 0.10
#calculates the amount that the cheer team gets
cheerTeamsProceed = netEarnings - studentGovernmentAssociation
#displays the amount that the SGA gets
print ('The student government associations earnings are: $',
format (studentGovernmentAssociation, ',.2f'), ".", sep="")
#displays the amount that the cheer team gets
print('The cheer teams earnings are: $',
format (cheerTeamsProceed, ',.2f'), ".", sep="")
#displays a congratulatory message if the cheer team gets more than $500
if (cheerTeamsProceed >= 500):
print ("Congratulations! You have raised $500 or more!")
#displays a sorry message if the cheer team gets less than $500
else:
print ("Sorry! You did not meet your goal! ")https://stackoverflow.com/questions/56170688
复制相似问题