Input a list of numbers from the user (space-separated), output the result of adding all the "even'th" elements and subtracting the odd
nums = input("Enter a list of numbers: ").split()
total = 0
for index, num in enumerate(nums):
if index % 2 == 0:
total += float(num)
else:
total -= float(num)
print("Total: {}".format(total))
Input a list of strings and a query string, output all indexes in which the query appears in the list
words = input("Enter a list of words: ").split()
query = input("Enter a query word: ")
result = []
for index, word in enumerate(words):
if word == query:
result.append(str(index))
if len(result) == 0:
print("Query string not found")
elif len(result) == 1:
print("Query found once: {}".format(result[0]))
else:
print("Query found in the following locations: {}".format(", ".join(result)))
Input two natural numbers (integers, >=0); find all natural numbers less than 100 that are multiples of both
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
mults1 = range(0, 100, num1)
mults2 = range(0, 100, num2)
result = []
for num in mults1:
if num in mults2:
result.append(str(num))
print("Common multiples: {}".format(", ".join(result)))
Input some number of natural numbers; find all natural numbers less than 100 that are multiples of all
nums = input("Enter numbers: ").split()
mults = []
for num in nums:
mults.append(range(0, 100, int(num)))
result = []
for num in mults[0]:
all_good = True
for mult_index in range(1, len(mults)):
if num not in mults[mult_index]:
all_good = False
if all_good:
result.append(str(num))
print("Common multiples: {}".format(", ".join(result)))
Let the user guess numbers (1-100) until they get the "secret" - each time tell them whether their guess was too high/low; when the user guesses correctly, output congratulations and how many guesses
secret = 77
done = False
count = 0
while not done:
guess = int(input("Guess a number (1-100): "))
count += 1
if guess > secret:
print("Too high")
elif guess < secret:
print("Too low")
else:
done = True
ending = "es"
if count == 1:
ending = ""
print("Congratulations - that took you only {} guess{}!".format(count, ending))
User enters income; given single tax bracket (https://www.nerdwallet.com/blog/taxes/federal-income-tax-brackets/), compute tax owed, highest bracket, effective rate
bracket = [ 12000, 0.37,
[[ 1, 9525, 0.10],
[ 9526, 38700, 0.12],
[ 38701, 82500, 0.22],
[ 82501, 157500, 0.24],
[157501, 200000, 0.32],
[200001, 500000, 0.35]]]
income = round(float(input("Enter income: ")))
agi = income - bracket[0]
if agi <= 0:
print("You do not owe any taxes!")
else:
taxes = 0
toprate = 0
for b in bracket[2]:
lowest_agi = b[0]
highest_agi = b[1]
rate = b[2]
if agi >= lowest_agi:
toprate = rate
if agi > highest_agi:
taxes += (highest_agi - lowest_agi + 1) * rate
else:
taxes += (agi - lowest_agi + 1) * rate
top_salary_limit = bracket[2][len(bracket[2])-1][1]
if agi > top_salary_limit:
toprate = bracket[1]
taxes += (agi - top_salary_limit) * toprate
pattern = "You are in the {:.0f}% bracket; you owe ${:.0f} in taxes for an effective rate of {:.0f}%."
print(pattern.format(toprate*100, taxes, 100 * (taxes / income)))