[英]Beginner Python Code Problem with the Mean Calculator function
For some reason in the code, I was testing whether it would work with two numbers, but the a
variable would be 2 once I typed it out, but the code said when I run it is that it is not 2. What is the problem?由于代码中的某种原因,我正在测试它是否可以使用两个数字,但是一旦我输入它, a
变量就会是 2,但是当我运行它时代码说它不是 2。有什么问题? Also I used Pycharm if that's part of the problem.如果这是问题的一部分,我也使用了 Pycharm。
def part1(Answer1, Answer2):
x=Answer1+Answer2
y=x/2
print(y)
print("What are your numbers? 2 max")
a=input("How many numbers would you like? :")
Answer1=input("What is your first number? :")
Answer2=input("What is your second number? :")
if a==2:
part1(Answer1, Answer2)
else:
print(a)
When using input()
the value returned is always a string.使用input()
时,返回的值始终是一个字符串。 So you will need to cast it as an integer using int(input())
.因此,您需要使用int(input())
将其转换为 integer 。 I'm not sure the purpose of the code, but based on the same logic I've refactored the code to be我不确定代码的用途,但基于相同的逻辑,我将代码重构为
def part1(Answer1, Answer2):
x = Answer1 + Answer2
y = x/2
print(y)
print("What are your numbers? 2 max")
a = int(input("How many numbers would you like? :"))
if a == 2:
Answer1 = int(input("What is your first number? :"))
Answer2 = int(input("What is your second number? :"))
part1(Answer1, Answer2)
else:
print(a)
Output: Output:
What are your numbers? 2 max
How many numbers would you like? : 2
What is your first number? : 3
What is your second number? : 5
4.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.