如何在python中打印while循环结束时的所有输出
我已经用list.append()函数解决了这个问题,但是我的导师告诉我只使用基本的python函数。这是我的密码:如何在python中打印while循环结束时的所有输出,python,python-3.x,Python,Python 3.x,我已经用list.append()函数解决了这个问题,但是我的导师告诉我只使用基本的python函数。这是我的密码: a = 0 b = 0 s = 0 x = str(s) print ('Enter the first number: ', end = '') c = input() a = int(c) finished = False while not finished: print ('Ente
a = 0
b = 0
s = 0
x = str(s)
print ('Enter the first number: ', end = '')
c = input()
a = int(c)
finished = False
while not finished:
print ('Enter the next number (0 to finish): ', end ='')
n = input()
b = int(n)
if b != 0:
if b == a:
x = ('Same')
elif b > a:
x = ('Up')
elif b < a:
x = ('Down')
a = b
s = x
else:
finished = True
print (str(x))
a=0
b=0
s=0
x=str(s)
打印('输入第一个数字:',结束='')
c=输入()
a=int(c)
完成=错误
未完成时:
打印('输入下一个数字(0完成):',结束='')
n=输入()
b=int(n)
如果b!=0:
如果b==a:
x=(‘相同’)
如果b>a:
x=(‘向上’)
elif b
我的目标是在while循环末尾的一行中打印(例如,在比较输入整数时上下相同)。让我知道如何改进我的代码。非常感谢您不确定您想要的结果是什么,但也许这是可行的:
a = 0
b = 99
result = ""
a = int(input('Enter the first number: '))
while b != 0:
b = int(input('Enter the next number (0 to finish): '))
if b == a:
result += ' Same'
elif b > a:
result += ' Up'
elif b < a:
result += ' Down'
a = b
print(result.strip())
您只需使用空字符串初始化
x
,然后继续连接到它
a = 0
b = 0
s = 0
x = ''
print('Enter the first number: ', end='')
c = input()
a = int(c)
finished = False
while not finished:
print('Enter the next number (0 to finish): ', end='')
n = input()
b = int(n)
if b != 0:
if b == a:
x += 'Same\n'
elif b > a:
x += 'Up\n'
elif b < a:
x += 'Down\n'
a = b
s = x
else:
finished = True
print(str(x))
a=0
b=0
s=0
x=''
打印('输入第一个数字:',结束='')
c=输入()
a=int(c)
完成=错误
未完成时:
打印('输入下一个数字(0完成):',结束='')
n=输入()
b=int(n)
如果b!=0:
如果b==a:
x+='相同\n'
如果b>a:
x+=“向上\n”
elif b
使用字符串连接,无需使用列表即可获得所需结果:
关于如何为您的程序执行此操作,我将给出两个提示:
x=str(s)
与
它不需要以字符串“0”开头,因为s是0,所以str(s)会这样做x=('SAME')
x=('UP')
x=('DOWN')
x=x+'SAME'
x=x+'UP'
x=x+'DOWN'
我删除了括号,因为它们不是必需的
至于样式,最好将变量命名为有用的东西,而不仅仅是字母。覆盖所有基础的if/else链中的最后一个stament应该是else。祝您好运,先生使用
list.append()
显示您的版本会很有帮助。你的导师有没有暗示他们想要什么list.append()
似乎没有那么奇特。a=0 b=0要求=[]打印('输入第一个数字:',end='')c=input()a=int(c)finished=False而未完成:打印('输入下一个数字,0完成:',end='')n=input()b=int(n)如果b!=0:如果b==a:requireds.append('Same'))elif b>a:requireds.append('Up'))elif bx=('SAME')
x=('UP')
x=('DOWN')
x=x+'SAME'
x=x+'UP'
x=x+'DOWN'