Sign in to follow this  
koka282

Can you explain to me this code !

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

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

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

I'm sorry but I have no idea what you're trying to say. What exactly is the problem now? Try to explain as specific as you can.

Share this post


Link to post
Share on other sites
To other people:
Since he is an ESL (incase if you don't know, English as Second Language) (so do I), I think we should explain to him in a simpler English or even chat to him in his native language.

To OP:
What language do you talk to your family?

(I know this is off-topic, just remind you guys about this)

EDITEED TO ADD TL;DR: KEEP IN MIND THAT THE OP IS AN ESL, EXPLAIN TO HIM WITH SIMPLER VOCALS.A

Share this post


Link to post
Share on other sites

please no down votes and understand me :)

when you type 100

it will check  if the number has 1 or 0

so the condition is true

so how_much = int(choice)

so i shouldn't get "You greedy bastard!"

 

Thanks :)

Share this post


Link to post
Share on other sites

when you type 100

it will check  if the number has 1 or 0

1. The script works fine, deal with it.

2. Try using 'print' on every variable that you're not sure on what value it is.

3. Your posts will keep getting downvoted as long as they doesn't contain any useful information/doesn't improve the discussion, etc. In simple words: you're spamming.

 

I'm done. good luck with your learning.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this