Public Group

# Can you explain to me this code !

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

## Recommended Posts

hi,

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:

print why, "Good job!"
exit(0)

gold_room()



Thanks

Edited by koka282

##### 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 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 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 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:

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

Edited by koka282

##### 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.

##### 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 on other sites

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:
& i should get that
if "0" in choice or "1" in choice:
how_much = int(choice)

Sorry for hard understanding

Thanks

1. 1
2. 2
3. 3
Rutin
20
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633655
• Total Posts
3013175
×