简体   繁体   English

如何使用字符串作为python中的结束符号?

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

How can I use a string to be the end symbol while I was prompting the user to input a specific integer '1' or specific string 'end' as the end symbol, just like the samples below?当我提示用户输入特定整数“1”或特定字符串“end”作为结束符号时,如何使用字符串作为结束符号,就像下面的示例一样? I now only can use '1' as the end symbol.我现在只能使用“1”作为结束符号。 I am a beginner, so please explain as simple as possible.我是初学者,所以请尽可能简单地解释。 Many thanks!非常感谢!

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))

sample1 output:样本 1 输出:

Enter a negative integer (1 or 'end' to end): -56输入一个负整数(1 或 'end' to end):-56

Enter a negative integer (1 or 'end' to end): -42输入一个负整数(1 或 'end' to end):-42

Enter a negative integer (1 or 'end' to end): -112输入一个负整数(1 或 'end' to end):-112

Enter a negative integer (1 or 'end' to end): -1输入一个负整数(1 或 'end' to end):-1

Enter a negative integer (1 or 'end' to end): -89输入一个负整数(1 或 'end' to end):-89

Enter a negative integer (1 or 'end' to end): -676输入一个负整数(1 或 'end' to end):-676

Enter a negative integer (1 or 'end' to end): -312输入一个负整数(1 或 'end' to end):-312

Enter a negative integer (1 or 'end' to end): 1输入一个负整数(1 或 'end' to end):1

The smallest integer is -676最小的整数是 -676

Sample2 output:样本 2 输出:

Enter a negative integer (1 or 'end' to end): -123输入一个负整数(1 或 'end' to end):-123

Enter a negative integer (1 or 'end' to end): -1输入一个负整数(1 或 'end' to end):-1

Enter a negative integer (1 or 'end' to end): -23输入一个负整数(1 或 'end' to end):-23

Enter a negative integer (1 or 'end' to end): -1144输入一个负整数(1 或 'end' to end):-1144

Enter a negative integer (1 or 'end' to end): -222输入一个负整数(1 或 'end' to end):-222

Enter a negative integer (1 or 'end' to end): -23输入一个负整数(1 或 'end' to end):-23

Enter a negative integer (1 or 'end' to end): -33输入一个负整数(1 或 'end' to end):-33

Enter a negative integer (1 or 'end' to end): -33输入一个负整数(1 或 'end' to end):-33

Enter a negative integer (1 or 'end' to end): end输入一个负整数(1 或 'end' to end):end

The smallest integer is -1144最小的整数是 -1144

Sample3 output:样本 3 输出:

Enter negative integers (1 to end): 1输入负整数(1 到结尾):1

No input!没有输入!

use this one, where 'end' is 1 as stop:使用这个,其中 '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.

相关问题 如何在 Python 字符串中提取# 符号后的单词 - How can I extract a word after # symbol in Python String 如何从Python中的字符串中删除以“:”结尾的所有单词? - How can I remove all words that end in “:” from a string in Python? 如何在Python中随机结束字符串并在末尾连接另一个字符串? - How can I end a string randomly and concatenate another string at the end in Python? 我可以在 Python 中的定义字符处结束字符串吗? - Can I end a string on a defined character in Python? 如何在 Python 中打印欧元 (€) 符号? - How can I print a euro (€) symbol in Python? 我试图确定一个字符串是否是一个问题。 我如何分析“?”符号(python) - I am trying to determine if a string is a Question. How can I analyze the “?” symbol (python) 如何在 Python 中制作经过验证的符号? - How can I make a verified symbol in Python? 如何将列表中的字符串用作 python 中的字符串方法? - How can I use the string in the list as string methods in python? 如何在XML内嵌的python脚本中使用大于或小于符号? - How can I use a greater than or less than symbol with a python script embedded in xml? Python:如何获取索引的负索引(str,beg = 0,end = len(string)),即从结束计数 - Python: how can I get negative indices out for index(str, beg=0, end=len(string)), i.e. counting from end
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM