Console Input
This page is under construction. Please come back later.
#!/usr/bin/env python3; ############################################################################### # This program demonstrates how to prompt the user for input. # # Copyright © 2020 Richard Lesh. All rights reserved. ############################################################################### import Utils # Begin Main name = Utils.prompt("What is your name? ") favoriteColor = Utils.prompt("What is your favorite color? ") print("Hello, {0:s}! I like {1:s} too!".format(name, favoriteColor))
Output
$ python ConsoleInput1.py < ../../examples/ConsoleInput1.in
What is your name? Rich
What is your favorite color? blue
Hello, Rich! I like blue too!
#!/usr/bin/env python3; ############################################################################### # This program demonstrates how to prompt the user for input. # # Copyright © 2020 Richard Lesh. All rights reserved. ############################################################################### import Utils # Begin Main try : favoriteIntInput = Utils.prompt("What is your favorite small integer? ") favoriteInt = int(favoriteIntInput) favoriteLongInput = Utils.prompt("What is your favorite large integer? ") favoriteLong = int(favoriteLongInput) favoriteDoubleInput = Utils.prompt("What is your favorite floating point? ") favoriteDouble = float(favoriteDoubleInput) sum = favoriteInt + favoriteLong + favoriteDouble print("All together they add up to {0:f}!".format(sum)) except ValueError as ex : print("Bad input! " + str(ex)) except Exception as ex : print("Don't know what went wrong!")
Output
$ python ConsoleInput2.py < ../../examples/ConsoleInput2.in1
What is your favorite small integer? 326
What is your favorite large integer? 1000000
What is your favorite floating point? 3.141926
All together they add up to 1000329.141593!
This always prints!
$ python ConsoleInput2.py < ../../examples/ConsoleInput2.in2
What is your favorite small integer? 326
What is your favorite large integer? abc
What is your favorite floating point? 3.141926
Bad input!
This always prints!
$ python ConsoleInput2.py < ../../examples/ConsoleInput2.in3
What is your favorite small integer? 326
What is your favorite large integer? 1000000
What is your favorite floating point? abc
Bad input!
This always prints!
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
References
- [[Python Language Reference]]
- [[Python Standard Library]]
- [[Python at TutorialsPoint]]