Really need help

python

#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
I do not understand why the sjekk_ svar doesnt print increment antall_kvinner by 1 when kjonn==f. Can please one of you help me?

<In what way does your code behave incorrectly? Include ALL error messages.>

<What do you expect to happen instead?>


antall_kvinner=0
antall_menn=0
antall_fag=0
antall_ITGK=0
antall_timer_lekser=0

alder=int(input("Hvor gammel er du? "))

def sjekk_svar():
    global alder
    global antall_kvinner
    global antall_menn
    global antall_ITGK
    global antall_fag
    while(alder<16 or alder>25):
        print("Du kan dessverre ikke ta denne undersøkelsen. ")
        alder=int(input("Hvor gammel er du? "))

    if(alder>16 and alder<25):
        kjonn=str(input("Skriv inn kjønnet ditt:f/m "))
        while(kjonn!="f" and kjonn!="M"):
            print("Skriv inn riktig kjønn")
            kjonn=str(input("Skriv inn kjønnet ditt:f/m "))
            if(kjonn=="f"):
                antall_kvinner+=1
                print(antall_kvinner)
            elif (kjonn=="m"):
                antall_menn+=1
                print(antall_menn)
        return 0

def les_ja_nei():
        global antall_timer_lekser
        global antall_fag
        global antall_ITGK
        fag=str(input("Tar du et fag? j/n"))
        while(fag!="j" and fag!="n"):
            fag=str(input("Tar du et fag? j/n"))
            if(fag=="j"):
                antall_fag+=1
                print(antall_fag)
        if(alder<22):
            variabelmedlem_ITGK=str(input("Tar du ITGK? j/n"))
            if(variabelmedlem_ITGK=="j"):
                antall_ITGK+=1
                print(antall_ITGK)
        elif(alder>22):
            variabelmedlem_ITGK=str(input("Tar du virkelig ITGK? j/n"))
            if variabelmedlem_ITGK=="j":
                antall_ITGK+=1
                print(antall_ITGK)
        else:
            return 0
        timer_lekser=int(input("Hvor mange timer bruker du i snitt daglig på lekser? "))
        antall_timer_lekser+=1
def den_store_undersokelsen():
    sjekk_svar()
    les_ja_nei()
    print(antall_menn)
    print(antall_kvinner)


den_store_undersokelsen()

#2

well, if you enter the right inputs the increase is printed:

Hvor gammel er du?  23
Skriv inn kjønnet ditt:f/m  x
Skriv inn riktig kjønn
Skriv inn kjønnet ditt:f/m  f
1
Tar du et fag? j/n 

i think you got this loop condition wrong:

while(kjonn!="f" and kjonn!="M"):

but its impossible for me to tell.

given i have no idea what anything means. so can’t deduce anything from variable names, i am guessing kjonn means gender


#3

that’s right kjonn means gender


#4

to print the increment of antall_kvinner we first need to enter a invalid gender, which doesn’t sound right. You will need to change that


#5

thanks, that helped :smiley:


#6

as i indicate in my original answer, i think it has to do with your while loop condition

Can you handle it from here?


#7

I have got one more question:

How can I give chance to the user to terminate the program why writing “bye” whenever she wants?
Do I have to change all of my previous code or is it possible to do it in another way?


#8

well, its easier to implement it from the start, because such decisions are fundamental on how your program is designed, to now implement this (and for me to give advise) i would need to tear the whole program apart

i would go for informing the user that they can press ctrl + c to exit the program, this will send an interrupt signal to the program.

you can catch this with try/except if you want.


#9

ok, then I will write in english and post it here :slight_smile:


#10

So here it is, I have no ide of how to give chance to the user to terminate the program. Please help me.

print("This is a survey for people between 16 and 25."
      "Please enter 'Bye' to quit, whenever you want to. ")
#undersokelse
number_of_women=0
number_of_men=0
number_of_subject=0
member_of_python=0
age=1
def survey():
    while True:
        try:
            x=int(input("How old are you? "))
            break
        except IOError:
            print("You are welcome again. ")
            break
    read_int()
    read_yes_or_no()


def read_int():
    global number_of_women
    global number_of_men
    age=int(input("How old are you?"))
    while(age<16 and age>25):
        print("You can not take this survey.")
        age=int(input("How old are you? "))

    if(age>16 and age<25):
        gender=str(input("What is your gender? (f/m) "))

        while(gender!="f" and gender!="m"):
            print("Wrong answer, your gender should be f or m")
            gender=str(input("What is your gender? (f/m)"))
        if(gender=="f"):
            number_of_women+=1
            print(number_of_women)
        elif(gender=="m"):
            number_of_men+=1
            print(number_of_men)
        else:
            return 0

def read_yes_or_no():
    global number_of_subject
    global member_of_python
    subject =str(input("Do you take a subject? y/n"))
    while(subject!="y" and subject!="n"):
        subject=str(input("Tar du et fag? y/n"))
    if(subject=="y"):
        number_of_subject+=1
        print(number_of_subject)
    if(age<22):
        variable_member_of_python=str(input("Do you take a course in Python? y/n"))
        if(variable_member_of_python=="y"):
            member_of_python+=1
            print(member_of_python)
        else:
            return 0

        spend_time_on_homework=input(input("How much time du you spend on your homework during a day?"))
        print(spend_time_on_homework)




survey()

#11

the problem is that you still want to implement something which might fundamentally change the design of your program.

Just put the whole code in a try except block, inform the user they can interrupt the program (ctrl + c), catch the exception and use sys.exit() to exit the program (don’t forget to important sys)

helping is one thing (for me), but for me, to come up with the best solution possible, i would need to break down the whole program, study the possible scenarios and so on, that just takes too much time


#12

thanks a lot for your help . :smiley: do you have tips to how can I get better in this? I do take courses here i code Academy and at school, what else should I do?


#13

study algorithms (introduction to algorithms (link)) is highly recommend

practice challenges on codewars


#14

one of the tings i don’t understand about your program, is this:

number_of_women=0
number_of_men=0
number_of_subject=0
member_of_python=0

what is the point? Once the script has executed, the count of this variable will be lost, so i don’t see the value of increasing them?


#15

Could be used to output to a text file in the future of the program, but I don’t see that happening in this code…


#16

I will use them in another function later in my program


#17

i am going to be honest, there you are making a novice mistake. Because then your program is loose bits and pieces and you get what is known as spaghetti code:

This exactly what you should avoid.

using global is something you should avoid for example

i think a database (sqlite3 fore example) might be an even better option, then you can easily execute operations like count the number of females (or males) and stuff, seems like this is where you are going


#18

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.