简体   繁体   English

为什么我的while循环不循环?

[英]Why isn't my while loop, looping?

I'm using notepad++ and I do this: 我正在使用记事本++,我这样做:

x = raw_input("blablabla") x = raw_input(“ blablabla”)

But it's printing onto the console, why is that? 但是它正在打印到控制台上,为什么呢? Shouldn't it just set the variable x to that command instead of printing? 它不应该只是将变量x设置为该命令而不是打印吗?

Though my problem isn't from this, I still don't understand why does a variable prompts without being called, shouldn't it be called only when I "print x"? 尽管不是我的问题,但我仍然不明白为什么变量会在不被调用的情况下提示,难道不应该仅在“打印x”时调用它吗? ====================EDIT======================= ===================编辑=======================

str = int(raw_input("Enter a number: "))

def numero(x):
    y = 0
    while x != 0:
        y += x
        return y
    else:
        print y

print numero(str)

Anyways, this isn't looping, my objective is here to add the numbers as long as user is inputing numbers until he inputs 0 which cancels and delivers the total.. I input a number and it goes right to printing the total without asking me for more numbers.. 无论如何,这不是循环的,我的目标是在用户输入数字之前就添加数字,直到他输入0为止,这会取消并提供总数。.我输入了一个数字,就可以不问我就打印总数为更多的数字..

It is normal behaviour. 这是正常的行为。 You want to get an input from user. 您想从用户那里得到输入。 It is just a prompt for user input. 它只是prompt用户输入。 Actually 'blabla..' isn't part of value of variable x . 实际上,“ blabla ..”不是变量x的值的一部分。

>>> x = raw_input("blablabla")
blablabla # waiting for further input...

Same behaviour in python command line. python命令行中的行为相同。

>>> x = raw_input("Please insert some text...\n")
>>> print x
Please insert some text...
My cool variable!
My cool variable! # current value of x

Just use as: 只需用作:

x = raw_input()

update: 更新:

I still don't understand why does a variable prompts without being called, shouldn't it be called only when I "print x"? 我仍然不明白为什么变量会在不被调用的情况下提示,不应该仅在我“打印x”时才调用它吗?

Printing of prompt initiated by call of raw_input('prompt') 打印通过调用raw_input('prompt')启动raw_input('prompt')

Try this: 尝试这个:

def numero():
    return int(raw_input("Enter a number: "))

x = numero()
y = 0
while x != 0:
    y += x
    x = numero()
else:
    print 'Total y:', y

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM