因此,例如,我必须进行如下输出:
Enter number of eggs: 19
1 dozen and 7 excess
相反,它显示如下:
Enter number of eggs: 19
1 dozen
它不显示多余
我尝试了这段代码:
eggs = int(input("Enter amount of eggs: "))
if eggs >= 1:
dozens = eggs // 12
excess = eggs % 12
print ("dozens eggs {0}".format(dozens))
print ("excess eggs {0}".format(excess))
else:
print('Invalid Input')
但是我的教授要求我提供确切的输出
eggs = int(input("Enter amount of eggs: "))
if eggs >= 1:
dozens = eggs // 12
excess = eggs % 12
print ("dozens eggs {0} ".format(dozens))
elif eggs >= 1 == 0:
print("{0} dozen and {1} excess".format(dozens, excess))
else:
eggs = 0
print("{0} excess".format(excess))
这是当前代码
我希望输出显示:
1 dozen and 7 excess
。但只显示一打 最佳答案
您是否尝试过运行第二段代码?它应该告诉您语法错误。第9行:“ elif”必须跟在“ if”之后。它不能在代码块的开头。
并且,“蛋> = 1 == 0”将始终为False。为了使它为真,它必须满足“蛋> = 1和1 == 0”。 (我建议在您的代码中使用“和”,这会更清楚,尤其是对于初学者。)
我似乎不明白“省略号”中的要点。 “ if”和“ else”似乎说明了所有可能的情况。鸡蛋将大于0或为0。0是一种特殊情况,因为它不允许除法。 (假设输入为负,则将使用“ elif eggs
我认为以下代码应该有效(对您的代码进行最少的更改)。
eggs = int(input(“输入鸡蛋数量:”))
if eggs >= 1:
dozens = eggs // 12
excess = eggs % 12
print ("{0} dozen eggs and {1} excess".format(dozens, excess))
else:
excess = 0
print("{0} excess".format(excess))
以下是我要写的内容,供您考虑(我有点完美主义者):
eggs = int(input("Enter amount of eggs: "))
if eggs > 0:
dozens = eggs // 12
excess = eggs % 12
if dozens == 0:
print(excess, "excess")
elif excess == 0:
print(dozens, "dozen eggs")
else:
print ("{} dozen eggs and {} excess".format(dozens, excess))
elif eggs < 0:
print("Amount can't be negative.")
else:
print("0 excess")
关于python - 我的输出需要像这样,它不会像我所需要的那样出现,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57028080/