我尝试创建一个函数,使用它来进行数学运算,如(加法和乘法),我能够提示值,当我插入值时,结果不会按预期返回。 enter image description here
代码
def addition(num1,num2):
return num1+num2 def
multiplication(num1,num2):
return num1*num2
print("1.addition")
print("2.multiplication")
choice = int(input("Enter Choice 1/2"))
num1 = float(input("Enter First Number:"))
num2 = float(input("Enter Second Number:"))
sum = float(num1)+float(num2)
multiply = float(num1)*float(num2)
if choice == 1:
print("additon of {0} and {1} is".format(num1,num2, sum))
elif choice == 2:
print("additon of {0} and {1} is".format(num1,num2, multiply))
答案 0 :(得分:0)
您缺少结果的位置说明符。由于您有三个参数,因此需要指定三个位置。
@Component({
selector: 'my-app',
template: `
<button (click)="dec()">-</button>
<button (click)="inc()">+</button>
<div [style]="style"> My height is set by CSS Variable </div>
`,
styles: [`
div {
height: var(--height);
}
`
]
})
export class AppComponent {
height = 50;
get style() {
return this.sanitizer.bypassSecurityTrustStyle(`--height: ${this.height}px`);
}
constructor(private sanitizer: DomSanitizer) { }
inc() {
this.height += 10;
}
dec() {
this.height -= 10;
if (this.height <= 0) {
this.height = 0;
}
}
}
答案 1 :(得分:0)
您不需要这些功能,也不需要sum
或multiply
个变量,只需将操作放在str.format()
中,您就错过了最后一个位置。
# def addition(num1,num2):
# return num1+num2
# def multiplication(num1,num2):
# return num1*num2
print("1.addition")
print("2.multiplication")
choice = int(input("Enter Choice 1/2"))
num1 = float(input("Enter First Number:"))
num2 = float(input("Enter Second Number:"))
# sum = float(num1)+float(num2)
# multiply = float(num1)*float(num2)
if choice == 1:
print("additon of {0} and {1} is {2}".format(num1,num2, num1 + num2))
elif choice == 2:
print("additon of {0} and {1} is {2}".format(num1, num2, num1 * num2))
并且知道你可以使用fstrings(&gt; = Python 3.6):
if choice == 1:
print(f"additon of {num1} and {num2} is {num1 + num2}")
elif choice == 2:
print(f"additon of {num1} and {num2} is {num1 * num2}")
旧式格式:
if choice == 1:
print("additon of %s and %s is %s" % (num1,num2, num1 + num2))
elif choice == 2:
print("additon of %s and %s is %s" % (num1, num2, num1 * num2))
或字符串连接:
if choice == 1:
print("additon of " + num1 + " and " + num2 + " is " + (num1 + num2))
elif choice == 2:
print("additon of " + num1 + " and " + num2 + " is " + (num1 * num2))
每个人都采用不同的方式,有时候了解所有这些方法很有用。