When coding in Tkinter, you can make a button, then when the button is pressed, it refers to something that you defined, or whatever its called when you use the def statement.

What I'm wondering, is if there is a way to do this in non Tkinter programing. I am making a text based game that requires a large section of code often, and I was wondering if I could put it off to the side, so to speak, and then whenever I need it, call it using the name I put in front with def.

For example: say I made a guess number program that lets two people take turns. the guess number code is:

import random
number = random.randint(1,10)
print "choose a number:"
while guess != number:
    num = num + 1
    guess = input("Enter guess " + str(num) + ": ")
    if guess < number:
        print "Higher..."
    elif guess > number:
        print "Lower..."
print "Yay you got it in",num,"tries"

That doesn't show the code for multiple players, but I was wondering if I could assign that section of code to some kind of statement that could be called up each time there is a new turn, kind of like how in Tkinter you can call something up every time a button is pressed.

I know this is confusing, I'm having a hard time explaining what I want to know.

I guess what I want is a good goto replacement, because I know python does not have a goto function. I just want to be able to point at other parts of my code, and then when that part is done, have it come back to where it was.

I've made progress in solving my own problem. I've come this far:

import random

def guess():
    global guess
    global num
    num = 0
    number = random.randint(1,10)
    while guess != number:
        num = num + 1
        guess = input("Enter guess " + str(num) + ": ")
        if guess < number:
            print "Higher..."
        elif guess > number:
            print "Lower..."
    return num

print "We will now determine who will go first..."
print "player 1, go first."

num = guess()

player1score = num

print player1score
print "now lets see player 2 try to beat that."

My problem comes at this point. I want player 2 to play too, using the same function, but I can't find a way to let player two play and record both their scores seperatly.

I was actually able to solve my own problem. I found a way to code a guess number game with multiple turns. But the actual guess number game code is only there once, its defined as a function and gets called each time a turn comes up. I couldn't figure out how to let player 2 go and record his score seperatly, but after about an hour.... I got it!

import random

def guess(y):
    global guess
    global num
    num = 0
    number = random.randint(1,10)
    while y != number:
        num = num + 1
        if y < number:
            y = input("higher... ")
        elif y > number:
            y = input("lower... ")
    return num

x = input("player 1's first guess? ")
guess(x)
player1score = num + 1

z = input("player 2's first guess? ")
guess(z)
player2score = num + 1

while player1score == player2score:
    print "tie breaker!"
    x = input("player 1's first guess? ")
    guess(x)
    player1score = num

    z = input("player 2's first guess? ")
    guess(z)
    player2score = num

if player1score < player2score:
    print "player 1 wins"
elif player1score > player2score:
    print "player 2 wins"

Solving a project's problems is really a good way to learn Python. You did well!

I have a brand new problem though.

firstTurnsCountries = [2, 3, 5, 7]
secondTurnsCountries = [0, 1, 4, 6]

attachedCountry0List = [1, 3]
attachedCountry1List = [0, 2, 3, 4]
attachedCountry2List = [1, 4]
attachedCountry3List = [0, 1, 4, 5]
attachedCountry4List = [1, 2, 3, 5, 6]
attachedCountry5List = [3, 4, 6, 7]
attachedCountry6List = [3, 4, 5, 7]
attachedCountry7List = [5, 6]

attachedCountryTotalList = [attachedCountry0List, attachedCountry1List, attachedCountry2List, attachedCountry3List, attachedCountry4List, attachedCountry5List, attachedCountry6List, attachedCountry7List]

def attachedCountryDetector(aFrom, aTo):
    global newAttackTo
    newAttackTo = 0
    while aTo not in attachedCountryTotalList[aFrom]:
        aFrom = input("pick again, country must be attached ")
        newAttackTo = aFrom
        if aTo in attachedCountryTotalList[aFrom]:
            print "okay..."
            newAttackTo = aFrom
            break
    return newAttackTo

attackFrom = input("attack from? ")
attackTo = input("attack? ")

attachedCountryDetector(attackFrom, attackTo)

print newAttackTo

Here is a sample of my code, I modified it to work standalone. The problem is that it doesn't seem stable. I want it to know that if the value aTo is not in the list, then to pick again, and it does that, but then if you tell it a value that IS in the list, certain ones won't work, but others will. for example, attachedCountry7List doesn't seem to work properly at all, while other lists will break out of the loop only for certain ones. It's hard to explain, but if you run this code and try a few different values, you'll see what I mean.

Okay I solved my own problem again, it was the aFrom variable that kept getting updated, it needed to be changed to aTo.

fixed code:

firstTurnsCountries = [2, 3, 5, 7]
secondTurnsCountries = [0, 1, 4, 6]

attachedCountry0List = [1, 3]
attachedCountry1List = [0, 2, 3, 4]
attachedCountry2List = [1, 4]
attachedCountry3List = [0, 1, 4, 5]
attachedCountry4List = [1, 2, 3, 5, 6]
attachedCountry5List = [3, 4, 6, 7]
attachedCountry6List = [3, 4, 5, 7]
attachedCountry7List = [5, 6]

attachedCountryTotalList = [attachedCountry0List, attachedCountry1List, attachedCountry2List, attachedCountry3List, attachedCountry4List, attachedCountry5List, attachedCountry6List, attachedCountry7List]

def attachedCountryDetector(aFrom, aTo):
    global newAttackTo
    newAttackTo = 0
    while aTo not in attachedCountryTotalList[aFrom]:
        aTo = input("pick again")
        newAttackTo = aTo
        if aTo in attachedCountryTotalList[aFrom]:
            print "okay..."
            newAttackTo = aTo
            break
    return newAttackTo

blah1080 = 1
while blah1080 == 1:
    attackFrom = input("attack from? ")
    attackTo = input("attack? ")

    attachedCountryDetector(attackFrom, attackTo)

    print "works!"
    print newAttackTo

Good thing you found the problem! There is also a line global newAttackTo that should not be there. Another thing that sticks out, you may have done that already, you need to do some hand holding for the user, giving an idea what range of numbers to input.

Good thing you found the problem! There is also a line global newAttackTo that should not be there. Another thing that sticks out, you may have done that already, you need to do some hand holding for the user, giving an idea what range of numbers to input.

yeah, i noticed that shouldnt be there too. And this is just a small section of my actual program, which is a text based version of risk. In the acutall program the user gets alot more guidance as to what to enter and when.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.