a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
print('The product of {first} and {sec} is: ',a*b).format(first = a, sec = b)
print('The sum of {first} and {sec} is: ',a+b).format(first = a, sec = b)
AttributeError 的情况即将到来。你们能解决这个问题吗?学校项目需要它。
我尝试了这个
placeholder
来自[https://www.w3schools.com/python/ref_string_format.asp]
您的参数顺序错误:
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
print('The product of {first} and {sec} is: '.format(first=a, sec=b), a*b)
print('The sum of {first} and {sec} is: '.format(first=a, sec=b), a+b)
不过,用
f-strings
会更好:
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
print(f'The product of {a} and {b} is: {a*b}')
print(f'The sum of {a} and {b} is: {a+b}')
输出:
Enter first number: 12
Enter second number: 4
The product of 12.0 and 4.0 is: 48.0
The sum of 12.0 and 4.0 is: 16.0