簡體   English   中英

如何在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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM