Python 混合数编程不起作用
这就是我目前编写程序的方式,因此,当一个人输入23作为分子,输入6作为分母时,会将解打印为一个混合数。但当我运行代码时,它仍然返回错误。我在什么地方出错了吗 请注意,内置功能与不同 您可能会遇到问题Python 混合数编程不起作用,python,Python,这就是我目前编写程序的方式,因此,当一个人输入23作为分子,输入6作为分母时,会将解打印为一个混合数。但当我运行代码时,它仍然返回错误。我在什么地方出错了吗 请注意,内置功能与不同 您可能会遇到问题TypeError:format()最多接受2个参数(给定3个),这是由 def main(): num = int(input("Enter the Numerator:")) den = int(input("Enter the Denominator:")) whole_num = num /
TypeError:format()最多接受2个参数(给定3个)
,这是由
def main():
num = int(input("Enter the Numerator:"))
den = int(input("Enter the Denominator:"))
whole_num = num // den
fract_num = num % den
print('The mixed number is {} and {}/{}', format (whole_num, fract_num,den))
main ()
要修复它,请将线路更换为
print('The mixed number is {} and {}/{}', format(whole_num, fract_num,den))
完整的源代码,(注意代码缩进)
尝试将逗号更改为点,如下所示
def main():
num = int(input("Enter the Numerator:"))
den = int(input("Enter the Denominator:"))
whole_num = num // den
fract_num = num % den
print('The mixed number is {} and {}/{}'.format (whole_num, fract_num, den))
main()
# Demo
$ python3 test.py
Enter the Numerator:23
Enter the Denominator:6
The mixed number is 3 and 5/6
什么错误?请提供回溯。缩进错误。您在字符串和
格式之间键入了,
,而不是
。如果您的代码没有运行,修复它的第一步是校对它,以确保您没有输入错误。
def main():
num = int(input("Enter the Numerator:"))
den = int(input("Enter the Denominator:"))
whole_num = num // den
fract_num = num % den
print('The mixed number is {} and {}/{}'.format (whole_num, fract_num, den))
main()
# Demo
$ python3 test.py
Enter the Numerator:23
Enter the Denominator:6
The mixed number is 3 and 5/6
print('The mixed number is {} and {}/{}'.format(whole_num, fract_num,den))