Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def ageCheck():
- birthYear = int(input('Your birth year: '))
- age = 2020 - birthYear
- print("You are %d years old" % (age))
- # This program takes the user's birth year as an integral input and stores it as a variable.
- # It then subtracts birthYear from 2020 and then prints it out in a statement.
- def downwardCount():
- n = 5
- while True:
- n = n - 10
- print(n)
- if keyboard.is_pressed('e'):
- break
- print("Please pick a new program.")
- # This program assigns the value 5 to n and runs an infinite loop subtracting 10 from n.
- # It then stops the program when the letter "e" is inputted and tells the user to choose a new program.
- def deskLength():
- while True:
- try:
- lengthInInches = int(input("Length of the desk in inches: "))
- lengthInCM = lengthInInches * 2.54
- print("The desk is %d centimetres long" % (lengthInCM))
- print("Please pick a new program.")
- except ValueError:
- print("Please print an integer.")
- continue
- else:
- return(lengthInInches)
- # This program tries to take an integral input as to how long the desk is in inches.
- # It then converts the inches to centimeters and outputs its length in a statement.
- # If a number is not inputted, it catches the ValueError and re-prompts the user to input an integer.
- def goodDay():
- x = 1
- while x <= 6:
- print("Have a good day")
- x += 1
- # This program assigns "x" the value of 1 then runs a loop saying to print the statement until x = 6.
- # After each output, it adds 1 to x.
- def ticketPrice():
- while True:
- try:
- age = int(input("Your age: "))
- if age < 14:
- print("You are a child and must pay $5.00.")
- elif age > 65:
- print("You are a senior and must pay $7.00.")
- else:
- print("You are an adult and must pay $9.00.")
- except ValueError:
- print("Please enter a numerical age.")
- continue
- else:
- return(age)
- # This program tries to take age as an input and stores it in a variable.
- # If the program catches a ValueError and the input isn't an integer, then it reprompts the user.
- # Depending on the value of the age inputted, it outputs a statement saying how expensive the user's movie ticket would be.
- def passOrFail():
- while True:
- try:
- grade = int(input("Your class mark: "))
- if grade >= 50:
- print("You passed the course")
- if grade < 50:
- print("You failed the course")
- except ValueError:
- print("Please enter a numerical grade.")
- continue
- else:
- return(grade)
- # This program attempts the user's school grade an input and stores it as the variable "grade"
- # 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.
- def speedCheck():
- while True:
- try:
- hours = get_numeric_input("Number of hours in trip")
- minutes = get_numeric_input("Number of additional minutes in trip")
- distance = get_numeric_input("Distance of trip in kilometers")
- speed = distance / (hours * 60 + minutes) * 60
- print("You are driving at %d km/h." % (speed))
- # This program takes 3 variables as inputs: hours, minutes and distance.
- # It then calculated the speed using the formula "speed = distance / time"
- # The hours are converted to minutes then added to the minutes, then converted to km/h at the end by multiplying by 60.
- # it uses the "get_numeric_input" function instead of "try" and "except"
- # This is because "try" and "except" resets all the variables and forces the user to re-input everything.
- def get_numeric_input(message):
- while True:
- try:
- return int(input(message))
- except ValueError:
- print("Please enter a whole number")
- # This is the companion function to speedCheck()
- # It is used instead of "try" and "except" individually because that is excess code and forces the user to re-input everything
- # creating a function instead of adjusting each variable individually is much easier and more efficient
- def wordCounter():
- wordList = []
- word = str(input("Enter your word: "))
- while word != "exit":
- wordList.append(word)
- word = str(input("Enter your word: "))
- wordCount = len(wordList)
- print("You inputted %d words" % (wordCount))
- # This function is similar to positiveIntegers() in that it stores inputs into a list.
- # It stores words in the list wordList
- # The function keeps prompting the user for words until the word "exit" is entered
- # It then tells the user how many words were entered and stops the program
- def integerSizing():
- positiveIntegers = []
- while True:
- try:
- while len(positiveIntegers) < 10:
- integer = int(input("Enter Positive Integer: "))
- if integer < 1:
- print("Integer must be positive")
- continue
- positiveIntegers.append(integer)
- positiveIntegers.sort()
- largest = positiveIntegers[-1]
- print("The largest number is %d" % largest)
- print("Please choose a new program")
- except ValueError:
- print("Please enter a positive integer.")
- continue
- else:
- return
- # This program takes positive integers as an input and stores them in the list, positiveIntegers.
- # If a positive integer is not inputted, then the user is re-prompted to enter one.
- # the "while" loop runs until positiveIntegers has 10 values in it
- # It sorts the inputted values and picks out
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement