![](/img/trans.png)
[英]How to manually take user input if user doesn't give voice input in python3
[英]How to give user input in python3
n=input("enter the no:")
check(test_list,n)
def check(test_list,n):
for i in test_list:
if(i==n):
print("yes in a list")
continue
else:
continue
我已經編寫了簡單的代碼來檢查是否為否。 是否存在於列表中,但是在接受用戶輸入時,我在提供輸入值 n 后沒有得到任何結果。
為什么會這樣?
在您的代碼中,第一行應更正為n=int(input("enter the no:"))
。
在python
中,它將inputs
作為strings
。 想一想如果您將input
作為 3。那么變量n
存儲值“3”(不是值 3)。 您應該需要知道3 == "3"
是False
。
因此,當您接受input
時,您應該將該字符串input
轉換為int
。 為此,我們使用int()
方法。 int()
方法將指定值轉換為integer數字
n=int(input("輸入編號:")) check(test_list,n)
def check(test_list,n):
for i in test_list:
if(i==n):
print("yes in a list")
continue
else:
continue
您沒有得到任何結果,因為 input() function 總是返回一個字符串。 出於這個原因,我們需要先將其轉換為 Integer,然后再將其傳遞到 function,我使用n=int(input())
進行了處理。
n=int(input("enter the no:"))
def check(test_list,n):
for i in test_list:
if(i==n):
print("yes in a list")
continue
else:
continue
check((1,2,3),n)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.