Python: how to make this code simpler?

Bratt Swan

I have some codes below, the choice takes only integer input but prints out something special if the input is not an integer. However, the codes below to treat this issue seems a little bit lengthy. Anyway to fix it?

from sys import exit

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

    choice = raw_input("> ")
    if "0" in choice or "1" in choice or "2" in choice or "3" in choice or "4" in choice or "5" in choice or "6" in choice or "7" in choice or "8" in choice or "9" 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(1)
    else:
        dead("You're greedy!")


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

gold_room()
D.Shawley

Try something like:

try:
    how_much = int(choice)
except ValueError:
    dead('Man, learn to type a number.')

and look up Easier to ask for forgiveness than permission for the rationale.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to make this code much simpler and shorter, Java

From Dev

Make jQuery code simpler

From Dev

How can I make this reflect based GO code simpler?

From Dev

How to make login page code with multiple users simpler?

From Java

How can I make this PyTorch tensor (B, C, H, W) tiling & blending code simpler and more efficient?

From Dev

How can this controller code be made simpler?

From Dev

Does the need to make the code simpler justify the use of wrong abstractions?

From Dev

How make a simpler expresion for showing multiple RecyclerView in one Activity

From Dev

What is the simplest python code to plot a simple graph (simpler than matlab)

From Dev

Is there a simpler way of writing this code?

From Dev

Is there a simpler way to make this expandable menu?

From Dev

How to make emacs automatically indent Python code?

From Dev

How to make my python code more effective?

From Dev

how to make this code on python run faster?

From Dev

How to make this Block of python code short and efficient

From Dev

How to make paragraphs readable within python code

From Dev

How to make this code work in python 2

From Dev

how to make this code on python run faster?

From Dev

How to make a python source code edit it self

From Dev

Why does my code to send e-mail from python looks much simpler than others?

From Dev

Is there a simpler way to code the following requirement and compress the code?

From Dev

Make sql query simpler shorter ( Laravel )

From Dev

How could this be written better/simpler?

From Dev

How to make my code detect end of string in python?

From Dev

How to make this code pick a random number each call Python?

From Dev

how to vectorize this python code to make it more efficient? (in speed)

From Dev

How to insert a line in python to make code easier to read

From Dev

in SciTE, how to make the whole block of code have less margin in python

From Dev

How to make my code detect end of string in python?

Related Related

  1. 1

    How to make this code much simpler and shorter, Java

  2. 2

    Make jQuery code simpler

  3. 3

    How can I make this reflect based GO code simpler?

  4. 4

    How to make login page code with multiple users simpler?

  5. 5

    How can I make this PyTorch tensor (B, C, H, W) tiling & blending code simpler and more efficient?

  6. 6

    How can this controller code be made simpler?

  7. 7

    Does the need to make the code simpler justify the use of wrong abstractions?

  8. 8

    How make a simpler expresion for showing multiple RecyclerView in one Activity

  9. 9

    What is the simplest python code to plot a simple graph (simpler than matlab)

  10. 10

    Is there a simpler way of writing this code?

  11. 11

    Is there a simpler way to make this expandable menu?

  12. 12

    How to make emacs automatically indent Python code?

  13. 13

    How to make my python code more effective?

  14. 14

    how to make this code on python run faster?

  15. 15

    How to make this Block of python code short and efficient

  16. 16

    How to make paragraphs readable within python code

  17. 17

    How to make this code work in python 2

  18. 18

    how to make this code on python run faster?

  19. 19

    How to make a python source code edit it self

  20. 20

    Why does my code to send e-mail from python looks much simpler than others?

  21. 21

    Is there a simpler way to code the following requirement and compress the code?

  22. 22

    Make sql query simpler shorter ( Laravel )

  23. 23

    How could this be written better/simpler?

  24. 24

    How to make my code detect end of string in python?

  25. 25

    How to make this code pick a random number each call Python?

  26. 26

    how to vectorize this python code to make it more efficient? (in speed)

  27. 27

    How to insert a line in python to make code easier to read

  28. 28

    in SciTE, how to make the whole block of code have less margin in python

  29. 29

    How to make my code detect end of string in python?

HotTag

Archive