Output should be displayed exactly as mentioned against each problem
General information/requirements/grading rules
- This homework tests your knowledge of Functions. So you need to use functions to get grades.
- Adhere to the naming conventions discussed in class for variable names, program name and function names
- Use meaningful names for variables
- If there are two words in the variable use first word lowercase and first letter of second wordupper case i.e., firstName or underscore between 2 words i.e first_name
- Include appropriate comments in the code
- Output should be displayed exactly as mentioned against each problem
- Indent the lines of code appropriately
- primeNum.py (20 points)
Write a program that displays all of the prime numbers from 1 through 10. The program should have a loop that calls the is_prime function.
Hints:
Declare local variables under main() program
For each number, print whether or not it is prime.
Write for loop, use range function to loop through the total numbers.
Call is_prime function
print if the number is prime or not prime
Define is_prime function, ‘def is_prime(number):’
The is_prime function receives a number as an argument, and returns True if number is prime, False otherwise.
Sample Output:
number is prime
————————
1 not prime
2 prime
3 prime
4 not prime
5 prime
6 not prime
7 prime
8 not prime
9 not prime
10 not prime
- calories.py (25 points)
Write a program that calculates following and display Grams of fat, Fat calories, Grams of carbs, Carb calories in 2 decimal positions.
the number of calories that result from the fat using formula:
Calories from fat = fat grams * 9
the number of calories that result from the carbohydrates using formula:
Calories from carbs = carb grams * 4
Hints:
Declare local variables under main() program
Prompt the user to enter grams_fat and grams_carbs
Calculate calories from fat
Calculate calories from carbs
Define a function to display resulting calories –
def showCarbs(gramsFat, gramsCarbs, caloriesFat, caloriesCarbs):
Note: This function will be called from main()
Sample Output:
If fat grams consumed entered is 87
If carbohydrate grams consumed entered is 39 then the output would be:
Grams of fat: 87.00
Fat calories: 783.00
Grams of carbs: 39.00
Carb calories: 156.00
- testscore.py (25 points)
Write a program that prompts the user to enter 5 test scores. The program should display a letter grade for each score and the average test score.
Hint:
Declare local variables under main() program
Prompts the user to enter 5 test scores
Define a function to calculate the average score: this should accept 5 test scores as argument and return the avg
Define a function to determine the letter grade: this should accept a test score as argument and return a letter grade based on the following grading scale.
Score Letter Grades
90-100 A
80-89 B
70-79 C
60-69 D
Below 60 F
Sample Output:
score numeric grade letter grade
—————————————————-
score 1: 99.0 A
score 2: 78.0 C
score 3: 68.0 D
score 4: 99.0 A
score 5: 89.0 B
—————————————————-
Average score: 86.6 B
4. randomGuess.py (30 points)
Write a program that generates a random number in the range of 1 through 20, and asks the user to guess what the number is.
If the user’s guess is higher than the random number, the program should display “Too high, try again”.
If the user’ guess is lower than the random number, the program should display “Too low, try again”.
If the user guesses the number, the application should congratulate the user and generate a new random number so the game can start over.
The game continues until the user chooses to quit by entering 0 as input anytime.
Sample Output:
Enter a number between 1 and 20, or 0 to quit: 14
Too high, try again
Enter a number between 1 and 20, or 0 to quit: 5
Too low, try again
Enter a number between 1 and 20, or 0 to quit: 10
Too low, try again
Enter a number between 1 and 20, or 0 to quit: 13
Congratulations! You guessed the right number!
Enter a number between 1 and 20, or 0 to quit: 10
Too high, try again
Enter a number between 1 and 20, or 0 to quit: 5
Too low, try again
Enter a number between 1 and 20, or 0 to quit: 7
Too low, try again
Enter a number between 1 and 20, or 0 to quit: 9
Congratulations! You guessed the right number!
Enter a number between 1 and 20, or 0 to quit: 0
Thanks for playing!