Skip to content

Instantly share code, notes, and snippets.

@rvente
Last active April 8, 2022 03:27
Show Gist options
  • Save rvente/05a7fa5018c6b3483366f6fb0f238314 to your computer and use it in GitHub Desktop.
Save rvente/05a7fa5018c6b3483366f6fb0f238314 to your computer and use it in GitHub Desktop.
value = None
running_sum = 0
num_positives = 0
num_negatives = 0
count = 0
while value != 0:
value = int(input("Enter an integer, the input ends if it is 0: "))
if value == 0:
break
running_sum = running_sum + value
count = count + 1
if value > 0:
num_positives = num_positives + 1
else:
num_negatives = num_negatives + 1
# out of loop
if count == 0 and value == 0:
print("No numbers entered except 0")
else:
average = running_sum / count
total = running_sum
print("The number of positives is", num_positives)
print("The number of negatives is", num_negatives)
print("The total is", total)
print("The average is", average)
principal = 10_000.0
for i in range(10):
principal = principal * 1.05
total = 0.0
print("The tuition after 10 years is", principal)
for i in range(4):
total += principal
principal = principal * 1.05
print("The total cost of tuition is", total)
"""
(Find the two highest scores)
Write a program that prompts the user to enter the number of students and
each student's name and score, and finally displays the student with the
highest score and the student with the second-highest score. Assume that the
number of students is at least 2.
Sample Run
Enter the number of students: 5
Enter a student name: Smith
Enter a student score: 60
Enter a student name: Jones
Enter a student score: 96
Enter a student name: Peterson
Enter a student score: 85
Enter a student name: Greenlaw
Enter a student score: 98
Enter a student name: Zhang
Enter a student score: 95
Top two students:
Greenlaw's score is 98.0
Jones's score is 96.0
"""
num_students = int(input('Enter the number of students: '))
best_student_name = ""
best_student_score = 0.0
snd_best_name = ""
snd_best_score = 0.0
for i in range(num_students):
student_name = input("Enter a student name: ")
student_score = float((input("Enter a student score: ")))
if student_score > best_student_score:
snd_best_name = best_student_name
snd_best_score = best_student_score
best_student_score = student_score
best_student_name = student_name
print("Top two students:")
print(best_student_name+"'s", "score is", best_student_score)
print(snd_best_name+"'s", "score is", snd_best_score)
"""
(Financial application: compare loans with various interest rates)
Write a program that lets the user enter the loan amount and loan period in
number of years and displays the monthly and total payments for each interest
rate starting from 5% to 8%, with an increment of 1/8.
Sample Run
Loan Amount: 10000
Number of Years: 5
Interest Rate Monthly Payment Total Payment
5.000% 188.71 11322.74
5.125% 189.29 11357.13
5.250% 189.86 11391.59
...
7.875% 202.17 12129.97
8.000% 202.76 12165.84
For the formula to compute monthly payment, see Live Example 2.8,
ComputeLoan.py.
"""
loan_amount = float(input("Loan amount: "))
num_years = int(input("Number of years: "))
interest_rate = 0.05
INCREMENT = 1/8 * .01
print("Interest Rate Monthly Payment Total Payment")
for i in range(num_years):
monthly_payment = 1/60 * loan_amount
loan_amount = (loan_amount - monthly_payment) * (1+interest_rate)
print(f"{interest_rate*100:.3f}%", f"{monthly_payment:.2f}", f"{loan_amount:.2f}")
interest_rate += INCREMENT
"""
(Count vowels and consonants)
Assume letters A, E, I, O, and U as the vowels. Write a program that prompts
the user to enter a string and displays the number of vowels and consonants in
the string.
Sample Run
Enter a string: Programming is fun
The number of vowels is 5
The number of consonants is 11
"""
user_message = input("Enter a string: ")
vowel_count = 0
consonant_count = 0
for c in user_message.lower():
if c == "a" or c == "e" or c == "i" or c == "o" or c == "u":
vowel_count += 1
elif c == " ":
continue
else:
consonant_count += 1
print("The number of vowels is", vowel_count)
print("The number of consonants is", consonant_count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment