0

This is program i made (basic mash);
I want to make it so that the program will ask if you want to repeat it again. "n" = exit. and anything else makes it repeat.....can someone help me? I'm not sure how to do it ...

x = 0 

import random

house =["Mansion", "Apartment", "Shack", "House"]
spouse =["EMPTY", "EMPTY", "EMPTY", "EMPTY"]
car =["EMPTY", "EMPTY", "EMPTY", "EMPTY"]
def dieroll():
    randomnumber = random.randrange(10)+1
    return randomnumber

print "Enter the name of three people you could marry: "
name1 = raw_input("Name1: ")
spouse[0]= name1
name2 = raw_input("Name2: ")
spouse[1]= name2
name5 = raw_input("Name3: ")
spouse[2]= name5

print "Enter the name of one person you wouldn't want to marry:"
name4 =raw_input("Name4: ")
spouse[3]= name4

print "Enter the name of 3 cars you want: "
car1 =raw_input("car1: ")
car[0]= car1
car2 = raw_input("car2: ")
car[1] = car2
car3=raw_input("car3: ")
car[2]=car3
print "Enter the name of one car you don't want: "
car4 =raw_input("car4: ")
car[3]=car4

#Results
print
print
print "You will live in a", random.choice(house)
print "Married to", random.choice(spouse)
print "While driving your", random.choice(car) 
print "With your", dieroll(), "kids in it!"

raw_input("\nPress the Enter key to continue")
4
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by StarZ
0

Generally you use a while loop you can break out of when done. I would use functions and pass arguments to streamline your code:

# random_game.py

import random

def dieroll():
    randomnumber = random.randrange(10)+1
    return randomnumber

def get_spouse(spouse):
    print "Enter the name of three people you could marry: "
    name1 = raw_input("Name1: ")
    spouse[0]= name1
    name2 = raw_input("Name2: ")
    spouse[1]= name2
    name5 = raw_input("Name3: ")
    spouse[2]= name5
    print "Enter the name of one person you wouldn't want to marry:"
    name4 =raw_input("Name4: ")
    spouse[3]= name4
    return spouse

def get_car(car):
    print "Enter the name of 3 cars you want: "
    car1 =raw_input("car1: ")
    car[0]= car1
    car2 = raw_input("car2: ")
    car[1] = car2
    car3=raw_input("car3: ")
    car[2]=car3
    print "Enter the name of one car you don't want: "
    car4 =raw_input("car4: ")
    car[3]=car4
    return car

def show_result(house, spouse, car):
    print
    print
    print "You will live in a", random.choice(house)
    print "Married to", random.choice(spouse)
    print "While driving your", random.choice(car) 
    print "With your", dieroll(), "kids in it!"
    print
    more = raw_input("Any more (yes or no)? ")
    return more.lower()

house = ["Mansion", "Apartment", "Shack", "House"]
spouse = ["EMPTY", "EMPTY", "EMPTY", "EMPTY"]
car = ["EMPTY", "EMPTY", "EMPTY", "EMPTY"]

while True:
    spouse = get_spouse(spouse)
    car = get_car(car)
    more = show_result(house, spouse, car)
    # the while loop will continue until you break out
    if 'n' in more:
        break
    print

print "Thanks for playing!"
raw_input("Press the Enter key to quit .. ")
0

But if you do really want to keep it in the procedure you have now, then os.startfile is what you need to look at.

#at the end of your code you put something like this
os.starfile(os.getcwd()+"\\Name of Python File.py")
#that will re-start your python file.
0

Yo. I've made a MASH thing as well. Try this one:

import random

house = ["a mansion", "an apartment", "a shack", "a house"]
spouse = ["name1", "name2", "name3", "name4"]
car = ["car1", "car2", "car3", "car4"]
kids = ["no kids", "1 kid", "2 kids", "5 kids"]

print"List 3 people you want to marry in the future:"
spouse[0] = raw_input("1. ")
spouse[1] = raw_input("2. ")
spouse[2] = raw_input("3. ")

print"List 1 person who you don't like: "
spouse[3] = raw_input("1. ")

print"List 3 cars you want in the future:"
car[0] = raw_input("1. ")
car[1] = raw_input("2. ")
car[2] = raw_input("3. ")

print"List 1 car that you don't like: "
car[3] = raw_input("1. ")

w = random.randrange(3)
x = random.randrange(3)
y = random.randrange(3)
z = random.randrange(3)

raw_input("Now mashing... Press [enter] for your results")
print "You will live in",house[w]
print "Married to",spouse[x]
print "Driving a",car[y]
print "With",kids[z],"running around the house"

raw_input("Press [Enter] to exit.")

You just need to use randoms like this.

0

lol, wow nice. Yours so much smaller than mine.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.