def input():
h = eval(input("Enter hours worked: \n"))
return h
def main():
hours = input()
print(hours)
main()
正如你所看到的,我是Python新手。我一直得到:「TypeError:input()只需要1個參數(給出0)。」任何幫助/解釋將不勝感激 - 非常感謝你!Python函數內返回eval值?
def input():
h = eval(input("Enter hours worked: \n"))
return h
def main():
hours = input()
print(hours)
main()
正如你所看到的,我是Python新手。我一直得到:「TypeError:input()只需要1個參數(給出0)。」任何幫助/解釋將不勝感激 - 非常感謝你!Python函數內返回eval值?
input()
是內建Python函數的名稱。
在你的代碼中,你重載它,這絕對不是一個好主意。嘗試命名你的函數別的東西:
def get_hours():
h = eval(input("Enter hours worked: \n"))
return h
def main():
hours = get_hours()
print(hours)
main()
這取決於Python版本,但OP似乎使用print作爲函數,這意味着Py3。但是,更重要的是,這並不能解釋OP獲得的特定錯誤。 – lvc
當你以後調用input
功能,可以定義一個在接受零個參數,然後第一線稱爲input
功能(我假設你想要它調用自帶的一個與Python並可能意外覆蓋),你傳遞它一個變量。
# don't try to override the buil-in function
def input_custom():
h = eval(input("Enter hours worked: \n"))
return h
def main():
hours = input_custom()
print(hours)
main()
因爲輸入是python中的方法,所以用不同的名稱更改輸入函數。
def inputx():
h = eval(input("Enter hours worked: \n"))
return h
def main():
hours = inputx()
print(hours)
main()
我不能複製你的確切的錯誤 - 而不是我得到:
TypeError: input() takes no arguments (1 given)
但是,你的錯誤可能是由同樣的事情引起的 - 當你的名字你的功能input
,你的影子內置-in input
:儘管你不期望提示,但Python無法看到兩者。如果你的名字你myinput
相反,Python可以看出其中的差別:
def myinput():
h = eval(input("Enter hours worked: \n"))
return h
def main():
hours = myinput()
print(hours)
main()
其他答案已經覆蓋了很多。我只是想增加一些想法吧。所有的函數名輸入 首先是所以首先
def my_input():
return input("Enter hours worked: ")
print my_input()
這應該足以覆蓋蟒蛇內置函數
。
理念:
現在,如果你正在使用Python的2.X版本那麼就沒有必要EVAL。
input():默認情況下Python會評估輸入表達式,如果它被python識別的話。
的raw_input():這裏,輸入取爲需要被評估的字符串。
在Python3.x的情況下:
輸入()行爲就像的raw_input()和的raw_input()被除去。
所以,你的代碼應該是
def my_input():
return float(input("Enter hours worked: "))
print(my_input())
它是更安全,更好的方式來獲取輸入,並還告訴我們,爲什麼不建議EVAL。
You don't know what's gonna come through that door.
謝謝。
輸入是一個Python中的方法,它需要1個參數。嘗試爲你的函數使用另一個名字'def input()' – Bob
謝謝islandmyth - 就是這樣。我重命名了這個函數,一切都很好。 – user1429845
不客氣。歡迎來到stackoverflow – Bob