I can not get the answers I need from the "duplicates"
I have made a while-loop that asks the user several questions, because I want the survey to run over again until the user inputs for the survey to break. I have made a function, and after every question, this function is ran through with the answer. If the answer says "break" (or "hade"), the while-loop should stop running
def check_answer(element):
if str(element) == "break":
return exit()
This function works, put it does not let the program put out the statistics of the program, due to quitting all together. I have found out that I am not allowed to
return break
so I don't know what to do. For reference, here is the entire code. Some of it is in Norwegian, but the relevant parts are the function, the while-loop, and the print-statements (the ones that wont be printed using quit())
I have also tried to use a True/False statement to fix this, but I have not succeeded:
def sjekk_svar(element):
if str(element) == "hade":
global to_continue
to_continue = False
return to_continue
As said, this will not work. Here is the full code:
def sjekk_svar(element):
if str(element) == "hade":
return quit()
while to_continue == True:
# Sex
kjonn = input("Er du mann eller kvinne? ")
*sjekk_svar(kjonn) # Calling the function
while kjonn != "mann" and kjonn != "kvinne":
kjonn = input("Er du mann eller kvinne? ")
sjekk_svar(kjonn) # Calling the function
if kjonn != "mann" and kjonn != "kvinne":
print("Feil input. Vennligst oppgi kjønn som mann eller kvinne")
# Age
alder = input("Hva er alderen din? ")
sjekk_svar(alder) # Calling the function
if (int(alder) > intervall_high or int(alder) < intervall_low):
print("Du er ikke innenfor aldersgruppen til denne undersøkelsen. Vennligst gi PC-en til noen andre.")
# Q1, 2 ,3
fag = input("Tar du noen universitetsfag? [ja/nei] ")
sjekk_svar(fag) # Calling the function
if fag == "ja" and int(alder) < 22:
itgk_medlem = input("Tar du faget ITGK? ")
sjekk_svar(itgk_medlem) # Calling the function
elif fag == "ja" and int(alder) >= 22:
itgk_medlem = input("Tar virkelig du ITGK? ")
sjekk_svar(itgk_medlem) # Calling the function
timer_lekser = input("Hvor mange timer om dagen bruker du i snitt på lekser? ")
sjekk_svar(timer_lekser) # Calling the function
# Start over again
print("Velkommen til ny spørreundersøkelse!")
# Assigning values of amount of surveys completed
ant_fag +=1
ant_timer += int(timer_lekser)
if kjonn == "mann":
menn += 1
if kjonn == "kvinne":
kvinner += 1
if itgk_medlem == "ja":
ant_itgk += 1
# Printing the statistics
print("Resultatet av spørreundersøkelsen er som følger:")
print("Antall kvinner:", str(kvinner))
print("Antall menn:", str(menn))
print("Antall personer som tar et fag:", str(fag))
print("Antall personer som tar ITGK:", str(ant_itgk))
print("Antall timer i snitt brukt på lekser:", str(ant_timer/(kvinner+menn)))