簡體   English   中英

如何使用字符串作為python中的結束符號?

[英]How can I use a string to be the end symbol in python?

當我提示用戶輸入特定整數“1”或特定字符串“end”作為結束符號時,如何使用字符串作為結束符號,就像下面的示例一樣? 我現在只能使用“1”作為結束符號。 我是初學者,所以請盡可能簡單地解釋。 非常感謝!

list = []
num = 0
stop = False

num = int(input('Enter a negative integer (1 or ‘end’ to end):'))
if num == 1:
    print('No input!')
else:
    list.append(num)
    print(list)
    while stop == False:
        num = int(input('Enter a negative integer (1 or ‘end’ to end):'))
        list.append(num)
        print(list)
        if num == 1:
            stop = True
            print('The smallest integer is', min(list))

樣本 1 輸出:

輸入一個負整數(1 或 'end' to end):-56

輸入一個負整數(1 或 'end' to end):-42

輸入一個負整數(1 或 'end' to end):-112

輸入一個負整數(1 或 'end' to end):-1

輸入一個負整數(1 或 'end' to end):-89

輸入一個負整數(1 或 'end' to end):-676

輸入一個負整數(1 或 'end' to end):-312

輸入一個負整數(1 或 'end' to end):1

最小的整數是 -676

樣本 2 輸出:

輸入一個負整數(1 或 'end' to end):-123

輸入一個負整數(1 或 'end' to end):-1

輸入一個負整數(1 或 'end' to end):-23

輸入一個負整數(1 或 'end' to end):-1144

輸入一個負整數(1 或 'end' to end):-222

輸入一個負整數(1 或 'end' to end):-23

輸入一個負整數(1 或 'end' to end):-33

輸入一個負整數(1 或 'end' to end):-33

輸入一個負整數(1 或 'end' to end):end

最小的整數是 -1144

樣本 3 輸出:

輸入負整數(1 到結尾):1

沒有輸入!

使用這個,其中 'end' 是 1 作為停止:

list = []
num = 0
stop = False

num = input('Enter a negative integer (1 or ‘end’ to end):')

if num=='end':
    num=1
else: num=int(num)

if num == 1:
    print('No input!')
else:
    list.append(num)
    print(list)
    while stop == False:
        num = input('Enter a negative integer (1 or ‘end’ to end):')
        if num=='end':
            num=1
        else: num=int(num)
        list.append(num)
        print(list)
        if num == 1:
            stop = True
            print('The smallest integer is', min(list))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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