Running a function x times in Python

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

Running a function x times in Python

Postby primal » Thu Aug 08, 2013 9:33 pm

Hi guys.

I'm completely new to programming, and am in the process of picking up Python, using 3.3 at the moment. As a fun exercise, and because I'm a complete perv, I wanted to make a sex position-game, where I'm using a random.randrange command, and assigning a position to each value generated, which is printed. At the beginning, I want to prompt the user to enter how many sex positions they want to be randomly picked for them, and then I want the CoinToss function to generate the corresponding amount of positions. How can I do that? At the moment, I have just repeated the same code 3 times, and the script will print 3 random positions, regardless of the user input at the start.

Also, I'm having trouble with the very end of the script. It asks the user if they want to run again, and I want it to do so only if the user enters yes (ja/Ja), otherwise exit. However, the program runs regardless of what is entered. Hint? And please speak to me in feed-with-teaspoon-language, as I still have a lot to learn. Hopefully both problems can be solved with commands I already know, even if it will be more complicated than what is necessary. I'm guessing my knowledge of the language shines through in the code below.

Code: Select all
import time
import random

constant = 1
FirstRun = True

def Intro():
    print ("Velkommen til sex-spillet! Dette spillet bestemmer hvilke sex-stillinger du skal prøve idag.")
    time.sleep(1)
    print (" ")
    print ("Hvor mange stillinger vil du prøve idag?")
    x = int(input())
    print ("Spillet vil nå plukke ut " + str(x) + " tilfeldige stillinger.")
    print (" ")
   
   
def CoinToss():
    print ("Spillet plukker ut tilfeldig sex-stilling...")
    time.sleep(1)
    print (".")
    time.sleep(1)
    print ("..")
    time.sleep(1)
    print ("...")
    time.sleep(1)
    print ("Første stilling du skal prøve er.....")
    time.sleep(2)
    result = random.randrange(9)
    if result == 1:
        print ("Misjonæren!")
    if result == 2:
        print ("Doggy style!")
    if result == 3:
        print ("Cowboy (jente på toppen)")
    if result == 4:
        print ("Omvendt cowboy!")
    if result == 5:
        print ("Spoon!")
    if result == 6:
        print ("Lotus!")
    if result == 7:
        print ("Misjonær med bena i været!")
    if result == 8:
        print ("Saksen!")
    if result == 9:
        print ("Planken!")
    time.sleep(2)

   

    print ("Spillet plukker ut neste tilfeldige sex-stilling...")
    time.sleep(1)
    print (".")
    time.sleep(1)
    print ("..")
    time.sleep(1)
    print ("...")
    time.sleep(1)
    print ("Andre stilling du skal prøve er.....")
    time.sleep(2)
    result = random.randrange(9)
    if result == 1:
        print ("Misjonæren!")
    if result == 2:
        print ("Doggy style!")
    if result == 3:
        print ("Cowboy (jente på toppen)")
    if result == 4:
        print ("Omvendt cowboy!")
    if result == 5:
        print ("Spoon!")
    if result == 6:
        print ("Lotus!")
    if result == 7:
        print ("Misjonær med bena i været!")
    if result == 8:
        print ("Saksen!")
    if result == 9:
        print ("Planken!")
    time.sleep(2)

    print ("Spillet plukker ut siste tilfeldige sex-stilling...")
    time.sleep(1)
    print (".")
    time.sleep(1)
    print ("..")
    time.sleep(1)
    print ("...")
    time.sleep(1)
    print ("Tredje stilling du skal prøve er.....")
    time.sleep(2)
    result = random.randrange(9)
    if result == 1:
        print ("Misjonæren!")
    if result == 2:
        print ("Doggy style!")
    if result == 3:
        print ("Cowboy (jente på toppen)")
    if result == 4:
        print ("Omvendt cowboy!")
    if result == 5:
        print ("Spoon!")
    if result == 6:
        print ("Lotus!")
    if result == 7:
        print ("Misjonær med bena i været!")
    if result == 8:
        print ("Saksen!")
    if result == 9:
        print ("Planken!")
    time.sleep(2)


while constant == 1:
    if FirstRun == True:
        Intro()
        FirstRun = False

    else:
        Answer = input()
        if Answer == "Ja" or "ja":
            CoinToss()
            print ("Er du misfornøyd med utvalget, og derfor prøve igjen?")

        else:
            exit()
primal
 
Posts: 2
Joined: Thu Aug 08, 2013 9:19 pm

Re: Running a function x times in Python

Postby Yoriz » Thu Aug 08, 2013 10:02 pm

Hi, welcome to the forum.
Have a study of this code and then modify it to do what you want.
Code: Select all
import random

choices = ('One', 'Two', 'Three', 'Four', 'Five', 'Six')

while True:
    times = int(input('how many times?'))
    for n in range(times):
        print(random.choice(choices))
    if input('run again?') not in ('Yes', 'yes', 'Y', 'y'):
        break


The problem with this part of your code
Code: Select all
if Answer == "Ja" or "ja":
is that it is not doing what you expect, it is checking if Answer == "Ja" is true/false or is "ja" true/false, "ja" will always be true
as shown here
Code: Select all
print(bool("ja"))
True

to check both conditions the code would need to be like the following
Code: Select all
if Answer == "Ja" or Answer == "ja":

Another way of doing this is to check if something is in a list of items like i done in my above example
Code: Select all
if Answer in ("Ja", "ja"):
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 871
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Running a function x times in Python

Postby primal » Thu Aug 08, 2013 10:41 pm

That was certainly an extremely easier way of doing that, and I learned some new commands at the same time. And best of all, it makes perfect sense. Thanks! :)
primal
 
Posts: 2
Joined: Thu Aug 08, 2013 9:19 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 2 guests