简体   繁体   中英

why does my code works in IDLE but not when I save the file and double click the file?

here is the code:

# Critter Caretaker
# A virtual pet to care for

class Critter(object):
    """A virtual pet"""
    def __init__(self, name, hunger = 0, boredom = 0):
        self.name = name
        self.hunger = hunger
        self.boredom = boredom

    def __pass_time(self):
        self.hunger += 1
        self.boredom += 1

    @property
    def mood(self):
        unhappiness = self.hunger + self.boredom
        if unhappiness < 5:
            m = "happy"
        elif 5 <= unhappiness <= 10:
            m = "okay"
        elif 11 <= unhappiness <= 15:
            m = "frustrated"
        else:
            m = "mad"
        return m

    def talk(self):
        print("I'm", self.name, "and I feel", self.mood, "now.\n")
        self.__pass_time()

    def eat(self, food = 4):
        print("Brrupp. Thank you.")
        self.hunger -= food
        if self.hunger < 0:
            self.hunger = 0
        self.__pass_time()

    def play(self, fun = 4):
        print("Whee!")
        self.boredom -= fun
        if self.boredom < 0:
            self.boredom = 0
        self.__pass_time()


crit_name = input("What do you want to name your critter?: ")
crit = Critter(crit_name)
choice = None
while choice != "0":

    print \
            ("""
            Critter Caretaker

            0 - Quit
            1 - Listen
            2 - Feed your critter
            3 - Play with your critter
            """)

    choice = input("Choice: ")
    print()

    # exit
    if choice == "0":
        print("Good-bye.")

    # listen to your critter
    elif choice == "1":
        crit.talk()

    # feed your critter
    elif choice == "2":
        crit.eat()

    # play with your critter
    elif choice == "3":
        crit.play()

    # some unknown choice
    else:
        print("\nSorry, but", choice, "isn't a valid choice.")


input("\n\nPress the enter key to exit.")

When I ran it in IDLE, it works totally fine but when I save the file and double click the file, it wont ran properly. For example, when I choose a valid choice from "0" - "3", it prints - "isn't a valid choice". but even if it's not a valid choice, it should print "Sorry, but - isn't a valid choice".

sorry for my English. Please do tell me if you are confused with my English.

By the way, I'm currently learning Python from a book called "Python Programming for Absolute Beginner" - by Michael Dawson. Should I finish this book or should I find another way to learn Python?

It looks a lot like your IDLE install has a different Python version compared to the one that is accessed elsewhere.

One thing you could do is add a check for the version at the top of the script to see what version you are running:

import sys
print(sys.version)

The other thing you can do is try to enter in a choice such as "3" with the string syntax and see if it works. If it does you are running a version of Python 2 when you try to run from outside IDLE.

The main thing that breaks if you are going from Python 3 to Python 2 is the behavior of the input() function. Python 3's input function is the same as the raw_input function in Python 2.

In Python 2 input just tries to execute whatever you enter as it if were valid Python code. In Python 3 input just stores whatever you entered in as a string.

So in Python 2:

choice = input("Choice: ")
Choice: 3
print(choice)
3

Makes choice be the integer 3. This will compare false in the checks you have because 3 == "3" will always be false. (Because any comparison with integer and string is False)

However the same code in Python 3 is a bit different:

choice = input("Choice: ")
Choice: 3
print(choice)
"3"

Here the code does what you would expect. Hopefully this explains how the code that uses Python 3.x in IDLE will work and the code when run by Python 2.x does not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM