Jump to content
  • Advertisement
Sign in to follow this  
koka282

Can you explain to me this code !

This topic is 1467 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Edited by koka282

Share this post


Link to post
Share on other sites
Advertisement

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

Edited by koka282

Share this post


Link to post
Share on other sites
    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.

Share this post


Link to post
Share on other sites

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.

Edited by Buckeye

Share this post


Link to post
Share on other sites

 

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

Edited by koka282

Share this post


Link to post
Share on other sites

 

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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!