帮助"def"在python3中**已解决** [英] Help with "def" in python3 **Solved**
问题描述
基本上我想发生的是用户输入一个数字,然后从所说的输入中减去一个数字.
解决方案1之后更新
我最初拥有的是
Basicly what i want to happen is the user inputs a number and one gets subtracted from said input.
updated after solution 1
What I originally had
b = input(int("pick a number")
def test(t):
t-1
return t
print(b test)
当我从终端运行它时,我得到:
When i run it from the terminal i get:
def test(t):
^
SyntaxError: invalid syntax
****************************************************** **********
我现在拥有的东西
************************************************************
What i have now
def test(t):
return t-1
b = input("pick a number")
print (test(b))
但是我仍然出现错误
However i still get an error
Traceback (most recent call last):
File "deftest2.py", line 5, in <module>
b = input(int("pick a number"))
ValueError: invalid literal for int() with base 10: ''pick a number''
</module>
我尝试从输入中删除"int",但是仍然出现错误.
谢谢您的时间.
I have tried taking out the "int" from input but i still get errors.
Thank you for your time.
推荐答案
这不是因为def
引起的.def
部分是正确的,除了一个错误:您不减去一个,因为相减的结果丢失了;您返回不变的t
.
应该是:
This is not because ofdef
. Thedef
part is correct, except one bug: you don''t subtract one, because the result of subtraction is lost; you return unchangedt
.
Should be:
def test(t):
return t - 1
显示该错误是由于前一行,这简直是乱码.很难理解您想如何获得b
.之所以显示def
上的错误,是因为前一行的括号不平衡.
从上下文中可以猜到您想从用户输入中获取b
.在这种情况下,您应该完成以下操作:
The error is shown due to the previous line, which is simply gibberish; hard to understand how did you want to get b
. The error on def
is shown because brackets on the previous line are not balanced.
From the context, one can guess that you wanted to get b
from user''s input. In this case, you should have done:
def test(t):
return t - 1
parameter = input("Input numeric parameter: ")
print test(parameter)
输入是字符串,因此您需要将其转换为int:
The input is a string, so that is what you need to convert to an int:
def test(t):
return t-1
b = int(input("pick a number"))
print(test(b))
这篇关于帮助"def"在python3中**已解决**的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!