我刚刚开始使用python,我正在尝试创建一个简单的Ricky计算器。
def ricky_adds(x,y):
num1 = x
num2 = y
if num1 >= 10:
num1 = input("No. I said A number, not multiple numbers dummy. Try again :")
if num1 < 0:
num1 = input("Wow you're dumb, I said a number, not some imaginary shit. Try again :")
num2 = y
if num2 >= 10:
num2 = input("No. I said A number, not multiple numbers dummy. Try again :")
if num2 < 0:
num2 = input("Wow you're dumb, I said a number, not some imaginary shit. Try again :")
result = num1 + num2
return result
ricky_adds(input("Gimmie a number. :"), input("Smokes, let's go. Another number, c'mon. :"))
但是收到此错误
Gimmie a number. :Smokes, let's go. Another number, c'mon. :
Traceback (most recent call last):
File "..\Playground\", line 17, in <module>
ricky_adds(input("Gimmie a number. :"), input("Smokes, let's go. Another number, c'mon. :"))
EOFError: EOF when reading a line
我无法弄清楚如何修复错误。我可以不使用“input()”来获取参数的用户输入吗?
答案 0 :(得分:0)
好的,谢谢大家的帮助到目前为止。这就是我现在所拥有的:
def main():
x = int(input("Gimmie a number. :"))
y = int(input("Smokes, let's go. Another number, c'mon. :"))
print("Here's the answer. See, Julian? I out smarted your stupid books and did that arthritis in my head and got " + str(ricky_adds(x,y)))
def ricky_adds(x,y):
num1 = x
num2 = y
lessthanzeroerror = "Wow you're dumb, I said a number, not some imaginary shit. Try again :"
greaterthantenerror = "No. I said A number, not multiple numbers dummy. Try again :"
if num1 >= 10:
num1 = int(input(greaterthantenerror + ":"))
elif num1 < 0:
num1 = int(input(lessthanzeroerror + ":"))
if num2 >= 10:
num2 = int(input(greaterthantenerror + ":"))
elif num2 < 0:
num2 = int(input(lessthanzeroerror + ":"))
result = num1 + num2
return result
main()
上一个答案: 所以我想出了如何使用Textwrangler。
我也改变了:
ricky_adds(input("Gimmie a number. :"), input("Smokes, let's go. Another number, c'mon. :"))
为:
print(ricky_adds(input("Gimmie a number. :"), input("Smokes, let's go. Another number, c'mon. :")))
它有效。谢谢大家的指导性问题。
答案 1 :(得分:0)
你需要重新认识你的逻辑。数字可以是&gt; = 10而不是多个数字。
考虑使用nested if statements
来实现您的目标。
def ricky_adds(x,y):
num1 = x
num2 = y
if num1 >= 10:
num1 = input("No. I said A number, not multiple numbers dummy. Try again :") # what happens after this statement? what is the purpse of this input. You need to nest the statements to continue in a logical manner.
if num1 < 0:
num1 = input("Wow you're dumb, I said a number, not some imaginary shit. Try again :")
if num2 >= 10:
num2 = input("No. I said A number, not multiple numbers dummy. Try again :")
if num2 < 0:
num2 = input("Wow you're dumb, I said a number, not some imaginary shit. Try again :")
以下是压缩代码的方法
if (num1>= 10 or num2 >= 10):
print("you have enter a number greater then 10")
data = input("try again")
if......
elif (num1 < 0 or num2 < 0):
print("you number is less than 0")
return (num1 + num2)
此外,您需要整数输入,而不是您拥有的。创建一个主要功能,可以帮助您实现这一目标。
def main():
x = int(input("Gimmie a number. :"))
y = int(input("Smokes, let's go. Another number, c'mon. :"))
ricky_adds(x,y)