• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Untitled a guest Feb 21st, 2020 113 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. def ageCheck():
2.     birthYear = int(input('Your birth year: '))
3.     age = 2020 - birthYear
4.     print("You are %d years old" % (age))
5.
6. # This program takes the user's birth year as an integral input and stores it as a variable.
7. # It then subtracts birthYear from 2020 and then prints it out in a statement.
8.
9. def downwardCount():
10.     n = 5
11.     while True:
12.         n = n - 10
13.         print(n)
14.         if keyboard.is_pressed('e'):
15.             break
16.             print("Please pick a new program.")
17.
18. # This program assigns the value 5 to n and runs an infinite loop subtracting 10 from n.
19. # It then stops the program when the letter "e" is inputted and tells the user to choose a new program.
20.
21. def deskLength():
22.     while True:
23.         try:
24.             lengthInInches = int(input("Length of the desk in inches: "))
25.             lengthInCM = lengthInInches * 2.54
26.             print("The desk is %d centimetres long" % (lengthInCM))
27.             print("Please pick a new program.")
28.         except ValueError:
30.             continue
31.         else:
32.             return(lengthInInches)
33.
34. # This program tries to take an integral input as to how long the desk is in inches.
35. # It then converts the inches to centimeters and outputs its length in a statement.
36. # If a number is not inputted, it catches the ValueError and re-prompts the user to input an integer.
37.
38. def goodDay():
39.     x = 1
40.
41.     while x <= 6:
42.         print("Have a good day")
43.         x += 1
44.
45. # This program assigns "x" the value of 1 then runs a loop saying to print the statement until x = 6.
46. # After each output, it adds 1 to x.
47.
48. def ticketPrice():
49.     while True:
50.         try:
51.             age = int(input("Your age: "))
52.
53.             if age < 14:
54.                 print("You are a child and must pay \$5.00.")
55.
56.             elif age > 65:
57.                 print("You are a senior and must pay \$7.00.")
58.
59.             else:
60.                 print("You are an adult and must pay \$9.00.")
61.
62.         except ValueError:
63.                 print("Please enter a numerical age.")
64.                 continue
65.         else:
66.             return(age)
67.
68. # This program tries to take age as an input and stores it in a variable.
69. # If the program catches a ValueError and the input isn't an integer, then it reprompts the user.
70. # Depending on the value of the age inputted, it outputs a statement saying how expensive the user's movie ticket would be.
71.
72. def passOrFail():
73.     while True:
74.         try:
76.
78.                 print("You passed the course")
79.
81.                 print("You failed the course")
82.         except ValueError:
84.             continue
85.         else:
87.
88. # This program attempts the user's school grade an input and stores it as the variable "grade"
89. # If the input is not a number and the program catches a ValueError, then it re-prompts the user until a proper input is given.
90.
91. def speedCheck():
92.     while True:
93.         try:
94.             hours = get_numeric_input("Number of hours in trip")
95.             minutes = get_numeric_input("Number of additional minutes in trip")
96.             distance = get_numeric_input("Distance of trip in kilometers")
97.
98.             speed = distance / (hours * 60 + minutes) * 60
99.
100.             print("You are driving at %d km/h." % (speed))
101.
102. # This program takes 3 variables as inputs: hours, minutes and distance.
103. # It then calculated the speed using the formula "speed = distance / time"
104. # The hours are converted to minutes then added to the minutes, then converted to km/h at the end by multiplying by 60.
105. # it uses the "get_numeric_input" function instead of "try" and "except"
106. # This is because "try" and "except" resets all the variables and forces the user to re-input everything.
107.
108. def get_numeric_input(message):
109.     while True:
110.         try:
111.             return int(input(message))
112.         except ValueError:
113.             print("Please enter a  whole number")
114.
115. # This is the companion function to speedCheck()
116. # It is used instead of "try" and "except" individually because that is excess code and forces the user to re-input everything
117. # creating a function instead of adjusting each variable individually is much easier and more efficient
118.
119. def wordCounter():
120.     wordList = []
121.
122.     word = str(input("Enter your word: "))
123.
124.     while word != "exit":
125.         wordList.append(word)
126.         word = str(input("Enter your word: "))
127.         wordCount = len(wordList)
128.
129.     print("You inputted %d words" % (wordCount))
130.
131. # This function is similar to positiveIntegers() in that it stores inputs into a list.
132. # It stores words in the list wordList
133. # The function keeps prompting the user for words until the word "exit" is entered
134. # It then tells the user how many words were entered and stops the program
135.
136. def integerSizing():
137.
138.     positiveIntegers = []
139.
140.     while True:
141.         try:
142.             while len(positiveIntegers) < 10:
143.                 integer = int(input("Enter Positive Integer: "))
144.                 if integer < 1:
145.                     print("Integer must be positive")
146.                     continue
147.
148.                 positiveIntegers.append(integer)
149.                 positiveIntegers.sort()
150.                 largest = positiveIntegers[-1]
151.
152.             print("The largest number is %d" % largest)
153.             print("Please choose a new program")
154.
155.         except ValueError:
156.             print("Please enter a positive integer.")
157.             continue
158.         else:
159.             return
160.
161. # This program takes positive integers as an input and stores them in the list, positiveIntegers.
162. # If a positive integer is not inputted, then the user is re-prompted to enter one.
163. # the "while" loop runs until positiveIntegers has 10 values in it
164. # It sorts the inputted values and picks out
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top