我似乎無法獲得多次顯示的用戶輸入。例如,如果輸入的是Python 2.7.5,在函數中使用循環並調用函數
Jeff 6
輸出應該
Jeff
Jeff
Jeff
Jeff
Jeff
Jeff
我新的功能在Python,但這裏是我的代碼至今:
def getName():
name = raw_input("please enter name")
return name
def getRepval():
irepnum = float(raw_input("please enter number to show name entered"))
return irepnum
def inamed(name, irepnum):
count = 1 #the loop to show the name entered by the user
while irepnum != count:
print name
count += 1 #do I need to use return??
def main(): #having the main func like this gives me an infinite loop
irepnum = 0
iname = getName() #I think my problem is somewhere here.
irepnum = getRepval()
inamed(irepnum,name)
main()
你的預期產出是什麼,你得到的是什麼產出?你說你得到這個名字有問題,但不要說出問題所在。我懷疑問題是當你調用inamed時irepnum <1,所以該函數中的循環永遠不會結束。對於像這樣的循環使用少於條件的做法是很好的做法,以防止可能出現的超出範圍的錯誤,例如使用'while count
我的錯,當我有一個無限循環時運行代碼 – JuanB457
我可以閱讀。你將不得不更具描述性。 –