Holmes
Holmes

Reputation: 5

Accessing user input taken in while loop (Python)

I want user to enter random numbers. I run a while loop until the user enters 0.

while input()!=0:
    print "Your number is:...??"

My question is: Are there any special variables (like $_ in Perl) using which I can access the user input? For example (in the above case) I want to print what the user has entered.

Upvotes: 0

Views: 154

Answers (2)

Jon Clements
Jon Clements

Reputation: 142106

An alternative method is to use iter and provide a callable:

for num in iter(lambda: int(raw_input('Your number is: ')), 0):
    print 'You entered', num

Upvotes: 1

wim
wim

Reputation: 362497

You need to assign the result to a variable:

s = None
while s != 0:
  s = int(input("Enter number: "))
  print("Your number is: {}".format(s))

This is a python3 example. For python 2.x you should be using raw_input instead.

Upvotes: 2

Related Questions