School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,003 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,964 people online right now. Registration is fast and FREE... Join Now!




Python Math Algorithm?

 

Python Math Algorithm?

jmesches

5 Sep, 2009 - 02:49 PM
Post #1

New D.I.C Head
*

Joined: 21 Feb, 2009
Posts: 4

Hey Coders,
I'm making a 'Pick-a-Number' Game, in Python. I need it to be if you correctly guess the number it will add how much your bet was to your money. I was wondering how to do this. Here's my code (Copyright):
CODE

import time
import random

print ".: Welcome To P.A.N! We Hope You Win! You Will Start With 100 Credits! Enjoy! :."
print "--- Created By: M4V3R1CK ---"
print ""
print "----------"
print ""
money2 = "100"
res1 = raw_input("Would You Like To Start The Game? Y/N: ")
while res1 == "y":
    print ""
    print "----------"
    print "Here Are The Numbers You May Pick:"
    print ""
    print "--"
    print ""
    print "=[ 1 ]="
    print "=[ 2 ]="
    print "=[ 3 ]="
    print "=[ 4 ]="
    print "=[ 5 ]="
    print ""
    print "--"
    print ""
    print "Place Your Bet (Be Sure Not To Include A Dollar Sign, e.g 29. Not $29"
    print "You currently Have: "
    print money2
    print "Credits"
    res2 = raw_input("Bet: ")
    print ""
    time.sleep(2)
    print "Bet Accepted!"
    print ""
    print "Now, Enter A Number Between 1 and 5"
    res3 = raw_input("Number: ")
    print ""
    time.sleep(2)
    print "Number Accepted!"
    print ""
    res4 = raw_input("Would You Like To See The Number? Y/N: ")
    if res4 == "n":
        time.sleep(1)
        print ""
        print "Well.. This Is A Waste Of Time!"
        time.sleep(3)
        exit();
    else:
        print ""
        print "Selecting A Number Please Wait..."
        print ""
        time.sleep(3)
        result = random.randint(1,5)
        if result == 1:
            if res3 == "1":
                print "=[ 1 ]="
                print ""
                print "Oh My Gosh! You Just Won:"
                print res2
                print "Credits!"
            else:
                print "=[ 1 ]="
                print ""
                print "Oh No! You Lost:"
                print res2
                print "Credits!"
        if result == 2:
            if res3 == "2":
                print "=[ 2 ]="
                print ""
                print "Oh My Gosh! You Just Won:"
                print res2
                print "Credits!"
            else:
                print "=[ 2 ]="
                print ""
                print "Oh No! You Lost:"
                print res2
                print "Credits!"
        if result == 3:
            if res3 == "3":
                print "=[ 3 ]="
                print ""
                print "Oh My Gosh! You Just Won:"
                print res2
                print "Credits!"
            else:
                print "=[ 3 ]="
                print ""
                print "Oh No! You Lost:"
                print res2
                print "Credits!"
        if result == 4:
            if res3 == "4":
                print "=[ 4 ]="
                print ""
                print "Oh My Gosh! You Just Won:"
                print res2
                print " Credits!"
            else:
                print "=[ 4 ]="
                print ""
                print "Oh No! You Lost:"
                print res2
                print "Credits!"
        if result == 5:
            if res3 == "5":
                print "=[ 5 ]="
                print ""
                print "Oh My Gosh! You Just Won:"
                print res2
                print "Credits!"
            else:
                print "=[ 5 ]="
                print ""
                print "Oh No! You Lost:"
                print res2
                print "Credits!"
        time.sleep(3)
        print ""
        print "----------"
        print ""
        res1 = raw_input("Would You Like To Play Again? Y/N: ")
print ""
time.sleep(1)
print "Okay Then! Bye!"
time.sleep(1)
exit();

Any Suggestions Are Helpful! Thanks!

User is offlineProfile CardPM
+Quote Post


Nallo

RE: Python Math Algorithm?

6 Sep, 2009 - 04:05 AM
Post #2

New D.I.C Head
*

Joined: 19 Jul, 2009
Posts: 24



Thanked: 3 times
My Contributions
money2 is the ammount of money your player has, res2 is the bet she made. So you want to add a multiple of res2 to money2 if she won. Unfortunately res2 is a string, so you cannot just write money2 = money2 + res2. you have to convert res2 to a integer first int_res2 = int(res2):
CODE

money2 = 100

#raw_input returns a string
res2 = raw_input("your wager: ")
#but we need an integer, so we convert it
int_res2 = int(res2)

if won:
    money2 = money2 + int_res2
else:
    money2 = money2 - int_res2

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 06:33AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month