混合数字编程不工作

时间:2016-05-23 00:36:05

标签: python

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 ()

目前这是我编写程序的方式,因此当一个人输入23作为分子时,6作为分母将解决方案打印为混合数字。但是当我运行我的代码时,它仍然会返回错误。我在某个地方犯了错误吗?

2 个答案:

答案 0 :(得分:1)

请注意,内置函数formatstr.format 不同。

您可能会遇到问题TypeError: format() takes at most 2 arguments (3 given),这是由

引起的
print('The mixed number is {} and {}/{}', format(whole_num, fract_num,den)) 

要修复它,请用

替换该行
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

答案 1 :(得分:0)

尝试将逗号更改为点,如下所示。

print('The mixed number is {} and {}/{}'.format(whole_num, fract_num,den))