Giving inputs

0 Arnab Banerjee · December 10, 2014
How do I give an input in python, like I do in C using scanf?:wassat:

Post a Reply

Replies

Oldest  Newest  Rating
+1 Vaggelis Theodoridis · December 11, 2014
a = input('Please give input:') # text parameter inside parenthesis isn't necessary 
0 Arnab Banerjee · December 11, 2014
Say I want to write an if-else program with giving inputs, then how shall I do that ?? :wassat:
+2 Vaggelis Theodoridis · December 11, 2014
well it depends what do u want to receive as input.
First of all you have to make a loop until the input is what you want, or in the "range" you want.
# say you want a number in the range of 1 to 10
a = None
while a not in range(1, 11):
 a = int(input("Give input:"))  # the moment i receive input, i convert it to integer

#BUT, user might type characters which cant be coverted to numbers, so you have to put a try/except in order for your program not to crash, so:
a = None
while a not in range(1, 11):
 try:
     a = int(input("Give input:"))
 except ValueError:
     print("Characters not allowed.")
# You can continue with more excepts to catch more errors if they might appear..

Now you cant continue safely with your input:
if a < 5:
  print("Your input is under 5")
elif a >= 5 and a <= 9:
  print("Your input is at least 5 and less than 10")
else:
 print("Your input is 10!")
  • 1

Python

103,705 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator