Can you explain to me this code !

Started by
15 comments, last by _the_phantom_ 9 years, 7 months ago

hi,

i was reading this code

Can you try to Run this code first & if it works in right way(Working properly) please explain to me it !!!


from sys import exit

def gold_room():
    print "This room is full of gold.  How much do you take?"

    choice = raw_input("> ") #ask the user !
    if "0" in choice or "1" in choice:
        how_much = int(choice)
    else:
        dead("Man, learn to type a number.")

    if how_much < 50:
        print "Nice, you're not greedy, you win!"
        exit(0)
    else:
        dead("You greedy bastard!")


def dead(why):
    print why, "Good job!"
    exit(0)


        
gold_room()

Thanks smile.png

Advertisement

Hi there.

This code is really simple, tell us explicitly which line do you have trouble understanding? raw_input? int()? if else? def? We cannot help you that way, and it's not the first time you know.

And yeah it works, it's copy-pasted from some tutorial if I recall.

it will run & i know it is simple but why when i type 55

the result:

Man, learn to type a number. Good job! (why i get this result )

why i the result not dead("You greedy bastard!")

Sorry Lysy but when i type the numbers i get many questions


    if "0" in choice or "1" in choice:
        how_much = int(choice)
    else:
        dead("Man, learn to type a number.")

That is a correct result for your input. Study this if-else clause, google/bing on what type raw_input returns, and how does 'in' keyword works on whatever type the 'choice' variable is.


if how_much < 50: print "Nice, you're not greedy, you win!" exit(0) else: dead("You greedy bastard!")

55 is not less than 50, so the else: dead(...) portion of that if/else statement is executed.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

it will run & i know it is simple but why when i type 55
the result:
Man, learn to type a number. Good job! (why i get this result )
why i the result not dead("You greedy bastard!")
Sorry Lysy but when i type the numbers i get many questions

if "0" in choice or "1" in choice:
    how_much = int(choice)
else:
    dead("Man, learn to type a number.")
the first line in that part checks if the text you've entered contains "0" or "1" , if it does it will try to convert the text to a number and assign it to the how_much variable , it the text doesn't contain 0 or 1 it will run the dead function with "Man, learn to type a number." as its parameter.

"55" doesn't contain 0 or 1 so it will run the dead function which prints the parameter "Man, learn to type a number." followed by "Good job!" and then it exits.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

it will run & i know it is simple but why when i type 55
the result:
Man, learn to type a number. Good job! (why i get this result )
why i the result not dead("You greedy bastard!")
Sorry Lysy but when i type the numbers i get many questions


if "0" in choice or "1" in choice:
    how_much = int(choice)
else:
    dead("Man, learn to type a number.")
the first line in that part checks if the text you've entered contains "0" or "1" , if it does it will try to convert the text to a number and assign it to the how_much variable , it the text doesn't contain 0 or 1 it will run the dead function with "Man, learn to type a number." as its parameter.

"55" doesn't contain 0 or 1 so it will run the dead function which prints the parameter "Man, learn to type a number." followed by "Good job!" and then it exits.

I know that but

else:
dead("You greedy bastard!")

55 isn't less than 50 so why we don't get dead("You greedy bastard!")

how it works ?

sorry for hard understanding

Thanks all smile.png

55 isn't less than 50 so when we don't get dead("You greedy bastard!")


def dead(why):
    print why, "Good job!"
    exit(0)

exit(0) ends up a script.

Please, read code first, google/bing it later.


if "0" in choice or "1" in choice:
        how_much = int(choice)
    else:
        dead("Man, learn to type a number.")
You are checking if the input has a 1 or 0. If you input "55" it does not have a 1 or 0, only 5's. This means that it will go to the "else" which calls dead("Man, learn to type a number"). dead(why) will print why + "good job". So if you call dead("Man, learn to type a number") then why will equal "Man, learn to type a number." And the print will then put "good job!" on the end which is your result. The rest of the code never gets run so it never compares 55 to 50.

Basically, the check whether the input is a number or not is terrible. You should replace

if "0" in choice or "1" in choice:
        how_much = int(choice)
    else:
        dead("Man, learn to type a number.")
With

try:
    how_much = int(choice)
except ValueError:
    print("Man, learn to type a number!")
    exit(0)

Lysy i read the code!!

ok i need answer for this question to be sure !

Try to type 100 you will get
You greedy bastard! Good job!
that's mean
else:
dead("You greedy bastard!")
& i should get that
if "0" in choice or "1" in choice:
how_much = int(choice)

Sorry for hard understanding

Thanks

This topic is closed to new replies.

Advertisement