Add exercise 8

This commit is contained in:
Oystein Kristoffer Tveit 2020-10-27 21:01:01 +01:00
parent 1c6bb26ee3
commit e1125351f7
5 changed files with 121 additions and 0 deletions

View File

@ -0,0 +1,51 @@
from random import randrange
songs = [("You hear the door slam. And realize there's nowhere left to", "run"),
("Oh, I wanna dance with somebody. I wanna feel the", "heat"),
("There's a fire starting in my heart. Reaching a fever", "pitch"),
("Hey, I just met you and this is crazy. But here's my", "number"),
("'Cause baby, you're a firework. Come on, show 'em what you're", "worth")]
# Om jeg tar inn songs som parameter vil ikke pop_random_songs kunne fjerne noe fra lista.
# Her velger jeg aktivt å ikke ta et argument inn med tanke på oppgave B
def pop_random_songs():
songIndex = randrange(len(songs))
song = songs[songIndex]
del songs[songIndex]
return song
def continueGuessing():
while True:
try:
answer = input('Do you want to go again? [y/n] ')
assert answer in ['y', 'Y', 'n', 'N']
return answer in ['y', 'Y']
except AssertionError:
pass
def song_contest():
currentSong = pop_random_songs()
while True:
print('The lyrics are:')
print(currentSong[0])
answer = input('What\'s the next word? ')
if answer.lower() == currentSong[1].lower():
print(f'Correct!')
if len(songs) == 0:
print('You did it! You remembered all the objects')
exit(0)
elif continueGuessing():
currentSong = pop_random_songs()
else:
print('Welcome back later :D')
exit(0)
else:
print('Wrong guess. Try again.')
song_contest()
# for _ in range(5):
# print(pop_random_songs())

View File

@ -0,0 +1,33 @@
def smallify_words(objects):
return [string.lower() for string in objects]
def get_5_objects():
while True:
try:
answer = input('Enter five objects separated by \';\': ').split(';')
assert len(answer) == 5
return answer
except AssertionError:
print(f'You were supposed to enter five objects, not {len(answer)}. Try again.')
def play_game():
objects = get_5_objects()
answer = input('What is your guess? ')
while True:
if answer == 'quit':
exit(0)
if answer in smallify_words(objects):
print(f'Congratulations! You remembered {answer}')
objects.remove(answer)
else:
print('Sorry, that was not one of the words')
if len(objects) == 0:
print('You did it! You remembered all the objects')
exit(0)
answer = input('What is your next guess? ').lower()
play_game()

View File

@ -0,0 +1,5 @@
def derivate(x, function):
h = 1e-8
return (function(x+h) - function(x)) / h
print(derivate(3, lambda x: x**2 + 2*x + 13))

View File

@ -0,0 +1,7 @@
def input_strings():
return [input('Skriv inn en streng: ') for _ in range(4) ]
def acronym():
return "".join([string[0].upper() for string in input_strings()])
print(acronym())

25
Exercise 8 - Inspera/7.py Normal file
View File

@ -0,0 +1,25 @@
def customInput(msg, interval):
while True:
try:
answer = int(input(msg))
assert answer in range(*interval)
return answer
except (AssertionError, ValueError):
print('You have to gie a value in the interval [1,10]. Try again')
def do_user_like(items):
print('On a scale of 1 to 10 where 10 is the highest, how much do you like:')
return [(item, customInput(f'{item}? ', (1,11))) for item in items]
def get_prioritized_list(lst):
return sorted(lst, key=lambda tuple: (tuple[1], tuple[0]), reverse=True)
def what_user_likes_best(items, num):
sortedList = get_prioritized_list(do_user_like(items))
print(f'Your top {num} are')
for i in range(num):
print(f'{i+1}. {sortedList[i][0]}')
x = what_user_likes_best(['dof', 'fas', 'be', 'aa'], 2)