Making a calculator with a game easter egged into it

This is the place for queries that don't fit in any of the other categories.

Making a calculator with a game easter egged into it

Postby spamynator_1 » Sat Mar 16, 2013 8:58 pm

I begun making a calculator a while ago which took all of five minutes, i wrote it in linux, now the file has been transferred to windows it no longer works. Based on the code as you can see the game initiates when you try to multiply 8 squared. that isnt finished and i would like input for why the calculator part no longer works... after entering in what function you want to do the program terminates and comes up with no errors.

Code: Select all
print("This is a calculator.")
print("The available functions are as follows:")
print("1. Add, 2. Subtract,3. Multiply,4. Divide,5. Exponent")
a= input("Enter the function you would like: ")
if a == (1):
    b= input("How many numbers would you like to add: ")
    if b == (1):
        print("How the fuck do you expect me to add one number?")
    elif b == (2):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        print(c + d)
    elif b == (3):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the thrid number: ")
        print(c + d + e)
    elif b == (4):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the third number: ")
        f= input("Enter the fourth number: ")
        print(c + d + e + f)
    elif b == (5):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the third number: ")
        f= input("Enter the fourth number: ")
        g= input("Enter the fifth number: ")
        print(c + d + e + f + g)
    elif b == (6):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the third number: ")
        f= input("Enter the fourth number: ")
        g= input("Enter the fifth number: ")
        h= input("Enter the sixth number: ")
        print(c + d + e + f + g + h)
    elif b == (7):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the third number: ")
        f= input("Enter the fourth number: ")
        g= input("Enter the fifth number: ")
        h= input("Enter the sixth number: ")
        i= input("Enter the seventh number: ")
        print(c + d + e + f + g + h + i)
    elif b == (8):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the third number: ")
        f= input("Enter the fourth number: ")
        g= input("Enter the fifth number: ")
        h= input("Enter the sixth number: ")
        i= input("Enter the seventh number: ")
        j= input("Enter the eighth number: ")
        print(c + d + e + f + g + h + i)
elif a == (2):
    b= input("How many numbers would you like to subtract: ")
    if b == (1):
        print("How the fuck do you expect me to subtract one number?")
    elif b == (2):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        print(c - d)
    elif b == (3):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the thrid number: ")
        print(c - d - e)
    elif b == (4):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the third number: ")
        f= input("Enter the fourth number: ")
        print(c - d - e - f)
    elif b == (5):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the third number: ")
        f= input("Enter the fourth number: ")
        g= input("Enter the fifth number: ")
        print(c - d - e - f - g)
    elif b == (6):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the third number: ")
        f= input("Enter the fourth number: ")
        g= input("Enter the fifth number: ")
        h= input("Enter the sixth number: ")
        print(c - d - e - f - g - h)
    elif b == (7):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the third number: ")
        f= input("Enter the fourth number: ")
        g= input("Enter the fifth number: ")
        h= input("Enter the sixth number: ")
        i= input("Enter the seventh number: ")
        print(c - d - e - f - g - h - i)
    elif b == (8):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the third number: ")
        f= input("Enter the fourth number: ")
        g= input("Enter the fifth number: ")
        h= input("Enter the sixth number: ")
        i= input("Enter the seventh number: ")
        j= input("Enter the eighth number: ")
        print(c - d - e - f - g - h - i)
elif a == (3):
    b= input("How many numbers would you like to multiply: ")
    if b == (1):
        print("How the fuck do you expect me to multiply one number?")
    elif b == (2):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        print(c * d)
    elif b == (3):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the thrid number: ")
        print(c * d * e)
    elif b == (4):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the third number: ")
        f= input("Enter the fourth number: ")
        print(c * d * e * f)
    elif b == (5):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the third number: ")
        f= input("Enter the fourth number: ")
        g= input("Enter the fifth number: ")
        print(c * d * e * f * g)
    elif b == (6):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the third number: ")
        f= input("Enter the fourth number: ")
        g= input("Enter the fifth number: ")
        h= input("Enter the sixth number: ")
        print(c * d * e * f * g * h)
    elif b == (7):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the third number: ")
        f= input("Enter the fourth number: ")
        g= input("Enter the fifth number: ")
        h= input("Enter the sixth number: ")
        i= input("Enter the seventh number: ")
        print(c * d * e * f * g * h * i)
    elif b == (8):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the third number: ")
        f= input("Enter the fourth number: ")
        g= input("Enter the fifth number: ")
        h= input("Enter the sixth number: ")
        i= input("Enter the seventh number: ")
        j= input("Enter the eighth number: ")
        print(c * d * e * f * g * h * i)
elif a == (4):
    b= input("How many numbers would you like to divide: ")
    if b == (1):
        print("How the fuck do you expect me to divide one number?")
    elif b == (2):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        print(c / d)
    elif b == (3):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the thrid number: ")
        print(c / d / e)
    elif b == (4):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the third number: ")
        f= input("Enter the fourth number: ")
        print(c / d / e / f)
    elif b == (5):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the third number: ")
        f= input("Enter the fourth number: ")
        g= input("Enter the fifth number: ")
        print(c / d / e / f / g)
    elif b == (6):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the third number: ")
        f= input("Enter the fourth number: ")
        g= input("Enter the fifth number: ")
        h= input("Enter the sixth number: ")
        print(c / d / e / f / g / h)
    elif b == (7):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the third number: ")
        f= input("Enter the fourth number: ")
        g= input("Enter the fifth number: ")
        h= input("Enter the sixth number: ")
        i= input("Enter the seventh number: ")
        print(c / d / e / f / g / h / i)
    elif b == (8):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the third number: ")
        f= input("Enter the fourth number: ")
        g= input("Enter the fifth number: ")
        h= input("Enter the sixth number: ")
        i= input("Enter the seventh number: ")
        j= input("Enter the eighth number: ")
        print(c / d / e / f / g / h / i)
elif a == (5):
    b= input("How many numbers would you like to multiply to the power of: ")
    if b == (1):
        print("1?")
    elif b == (2):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        print(c ** d)
        if c == (8):
            if d == (3):
                print("You find yourself alone in the wilderness, confused.")
                import random
                class Info:
                    xloc = 0
                    zloc = 0
                    health = 100
                    stamina = 100
                    magica = 100
                    strength = 100
                    endurance = 100
                    def showinfo(self):
                        w= Info()
                        print(w)
                    #Position Changes
                    def walkn(self):
                        Info.zloc += 1
                    def walks(self):
                        Info.zloc -= 1
                    def walke(self):
                        Info.xloc += 1
                    def walkw(self):
                        Info.xloc -= 1
                    #Health Starts
                    def lowdmg(self):
                        Info.health -= 5
                    def meddmg(self):
                        Info.health -= 15
                    def highdmg(self):
                        Info.health -= 35
                    def addhlthlow(self):
                        Info.health += 5
                    def addhlthmed(self):
                        Info.health += 15
                    def addhlthhig(self):
                        Info.health += 35
                    #Stamina Starts
                    def dmgstmlow(self):
                        Info.stamina -= 5
                    def dmgstmmed(self):
                        Info.stamina -= 15
                    def dmgstmhig(self):
                        Info.stamina -= 35
                    def addstmlow(self):
                        Info.stamina += 5
                    def addstmmed(self):
                        Info.stamina += 15
                    def addstmhig(self):
                        Info.stamina += 35                 
                    #Magica Starts
                    def dmgmaglow(self):
                        Info.magica -= 5
                    def dmgmagmed(self):
                        Info.magica -= 15
                    def dmgmaghig(self):
                        Info.magica -= 35
                    def addmaglow(self):
                        Info.magica += 5
                    def addmagmed(self):
                        Info.magica += 15
                    def addmaghig(self):
                        Info.magica += 35
                    #Strength Starts
                    def dmgstrlow(self):
                        Info.strength -= 5
                    def dmgstrmed(self):
                        Info.strength -= 15
                    def dmgstrhig(self):
                        Info.strength -= 35
                    def addstrlow(self):
                        Info.strength += 5
                    def addstrmed(self):
                        Info.strenght += 15
                    def addstrhig(self):
                        Info.strength += 35
                    #Endurance Starts
                    def dmgendlow(self):
                        Info.endurance -= 5
                    def dmgendmed(self):
                        Info.endurance -= 15
                    def dmgendhig(self):
                        Info.endurance -= 35
                    def addendlow(self):
                        Info.endurance += 5
                    def addendmed(self):
                        Info.endurance += 15
                    def addendhig(self):
                        Info.endurance += 35
    elif b == (3):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the thrid number: ")
        print(c ** d ** e)
    elif b == (4):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the third number: ")
        f= input("Enter the fourth number: ")
        print(c ** d ** e ** f)
    elif b == (5):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the third number: ")
        f= input("Enter the fourth number: ")
        g= input("Enter the fifth number: ")
        print(c ** d ** e ** f ** g)
    elif b == (6):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the third number: ")
        f= input("Enter the fourth number: ")
        g= input("Enter the fifth number: ")
        h= input("Enter the sixth number: ")
        print(c ** d ** e ** f ** g ** h)
    elif b == (7):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the third number: ")
        f= input("Enter the fourth number: ")
        g= input("Enter the fifth number: ")
        h= input("Enter the sixth number: ")
        i= input("Enter the seventh number: ")
        print(c ** d ** e ** f ** g ** h ** i)
    elif b == (8):
        c= input("Enter the first number: ")
        d= input("Enter the second number: ")
        e= input("Enter the third number: ")
        f= input("Enter the fourth number: ")
        g= input("Enter the fifth number: ")
        h= input("Enter the sixth number: ")
        i= input("Enter the seventh number: ")
        j= input("Enter the eighth number: ")
        print(c ** d ** e ** f ** g ** h ** i)
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: Making a calculator with a game easter egged into it

Postby ichabod801 » Sat Mar 16, 2013 10:51 pm

What version are you running it under in the two machines? I ask because input works differently in 2.x and 3.x, and you're whole program is based on input.

If that's not your problem, please be more clear about what your problem is. Stylistically, your program has problems. You have the same things being done repeatedly. You shouldn't do that. You should create loops and functions to do that for you.
Craig "Ichabod" O'Brien
Minimalist, buddhist, theist, and programmer
Current languages: Python, SAS, and C++
Previous serious languages: R, Java, VBA, Lisp, HyperTalk, BASIC
ichabod801
 
Posts: 84
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC

Re: Making a calculator with a game easter egged into it

Postby snippsat » Sun Mar 17, 2013 12:21 pm

As pointed out by @ichabod801 you have a lot of repeating code,this is not good at all.
You should create loops and functions to do that for you.

Just to show you some code that do this,pay attention to function calculate(operator)
this function take operator as argument(+-*/) and do all calculation for Add,Subtract... and so on,no repeating code.
Code: Select all
def showoptions():
     print("1. Addition")
     print("2. Division")
     print("3. Subtraction")
     print("4. Multiplication\n")

def get_option():
    ops = {'1': lambda x, y: x + y, '4': lambda x, y: x * y,
           '2': lambda x, y: x / y, '3': lambda x, y: x - y}
    while True:
        showoptions()
        option = input("Please Make Your Selection: ")
        if option in ops:
            return ops[option]
        else:
            print("'{}' is not a valid option,Try again\n".format(option))

def calculate(operator):
    val1 = float(input("Please Enter Value 1: "))
    val2 = float(input("Please Enter Value 2: "))
    return operator(val1,val2)

def main():
    operator = get_option()
    print('Result is: {}'.format(calculate(operator)))

if __name__ == '__main__':
    main()
User avatar
snippsat
 
Posts: 136
Joined: Thu Feb 21, 2013 12:04 am

Re: Making a calculator with a game easter egged into it

Postby micseydel » Sun Mar 17, 2013 9:28 pm

I looked at the code, and like ichabod I suspect that you're using Python 2 when it works, and Python 3 doesn't it doesn't. It's good style though in Python 2 to use raw_input() instead of input(), and to explicitly convert the string you get from raw_input() into a number using int() or float(). You can make a simple program like this backward compatible by putting at the top of your code
Code: Select all
try:
    input = raw_input # python 2
except NameError:
    pass # python 3

I didn't look at all 45 instances of print() in your code but you might need to import Python 3's print function in Python 2 to make it work properly as well.

Now, I will say things unrelated to answering your question, but which you may find useful.

On top of snippsat's answer, you can also get from the operator module the functions you need, instead of using lambda. You'd just import add, sub, mul, div and pow from it and use them in place of the lambdas. You could also easily support multiple operands with reduce(). Note also that div() does integer division and you might want truediv() which does more expected math.

Just to echo and reinforce the sentiment we've seen in the rest of this thread: if you're writing virtually the same code more than twice (and oftentimes even just twice) then you need to change your code to not repeat yourself. Functions, loops and lists are definitely the way to go there. Enumerated variables, such as "a1, a2, a3, ..." or "a, b, c, d, e, ..." typically indicate that you should use a list instead. Using loops, you could also easily handle an arbitrary number of numbers from the user, whereas you currently have hard-coded a fixed upper bound.

We do this so that there is less code to read, and when you make an update, it's easy to make it in one place instead of several. When looking at a monstrous amount of code (for what it does) like what you've posted, it can become much harder to debug than the version written properly. This is the case for example in your class that is buried away in your code, if you wanted to update the 5/15/35 values as it is written it would be very difficult, whereas if you'd assigned them to constant variables at the beginning of the program you could reference them later.

There are other issues here: the parenthesis around single numbers are unnecessary and generally considered "noise", the class and import deep in your program would be recommended to be at the top level instead of buried deep in the code, your class seems to be a strange attempt at a singleton but does strange things and that code currently doesn't do anything. You typically shouldn't post code in a situation like this where a part has nothing to do with the execution of the program, or the error you're getting.

I would encourage you to take this advice and try to write the program well, repost it here, and have us help you to refine it. I believe the exercise would be very helpful for you. You could start with the code snippsat provided, and update it to do what your program does (and more!).
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1131
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], gutschy, micseydel and 3 guests