Reputation: 13
I'm very new at Python, and programming in general, so please try not to be frustrated.
I'm to make a program (at a friend's request, he's paying me, so don't judge) that would simplify games like truth or dare by making selection of names/dares random. It was working well when I just made it select a person from a group of two boys and two girls, and then select a random dare from an extensive list my friend created. However, I wanted to make it so you could enter specific names, and decide how many names, instead of selecting persons 1-4. I may have vastly overcomplicated it with commands that I had simply Googled and didn't take the time to understand, and now it gives errors for almost every change I make. Now, it's saying that the variable "boys" is undefined, when I did (at least I think I did) earlier. I think that even when I fix "boys," "girls" will have the same problem. Any and all help you can offer with simplification, rectifying errors, and anything else would be greatly appreciated. I know it's long and complicated, but any help at all would be really phenomenal.
import random
print "Now, important note, all answers must be made in quotes. I know, it sucks, but that's the way it is and I cannot change it."
print "Ready? [y/n]"
repeat = input()
while repeat == "y":
print "Are there any boys playing? [y/n]"
boys1 = input()
boys2 = "n"
boys3 = "n"
boys4 = "n"
if boys1 == "n":
repeat == "n"
if boys1 == "y":
print "What is the first boy's name?"
boy1 = input()
boy2 = ""
boy3 = ""
boy4 = ""
print "Are there any more?"
boys2 = input()
boys3 = "n"
boys4 = "n"
if boys2 == "y":
print "What is his name?"
boy2 = input()
boy3 = ""
boy4 = ""
print "Are there any more?"
boys3 = input()
boys4 = "n"
if boys3 == "y":
print "What is his name?"
boy3 = input()
boy4 = ""
print "Are there any more?"
boys4 = input()
if boys4 == "y":
print "What is his name?"
boy4 = input()
print "The maximum number of boys is four."
print "Are there any girls playing? [y/n]"
girls1 = input()
girls2 = "n"
girls3 = "n"
girls4 = "n"
if girls1 == "n":
repeat == "n"
if girls1 == "y":
print "What is her name?"
girl1 = input()
girl2 = ""
girl3 = ""
girl4 = ""
print "Are there any more?"
girls2 = input()
girls3 = "n"
girls4 = "n"
if girls2 == "y":
print "What is her name?"
girl2 = input()
girl3 = ""
girl4 = ""
print "Are there any more?"
girls3 = input()
girls4 = "n"
if girls3 == "y":
print "What is her name?"
girl3 = input()
girl4 = ""
print "Are there any more?"
girls4 = input()
if girls4 == "y":
print "What is her name?"
girl4 = input()
print "The maximum number of girls is four."
if not boys1 == "y":
print "There are no boys. The program cannot work."
if not boys2 == "y":
boys = [boy1]
if not 'boys3' == "y":
boys = [boy1, boy2]
if not boys4 == "y":
boys = [boy1, boy2, boy3]
if boys4 == "y":
boys = [boy1, boy2, boy3, boy4]
if not girls1 == "y":
print "There are no girls. Your party sucks."
if not girls2 == "y":
girls = [girl1]
if not girls3 == "y":
girls = [girl1, girl2]
if not girls4 == "y":
girls = [girl1, girl2, girl3]
if girls4 == "y":
girls = [girl1, girl2, girl3, girl4]
print random.sample(boys, 1)
print "must perform the action on"
print random.sample(girls, 1)
actions = ["A very long and extensive list of various actions.", "Each in their own section"]
print random.sample(actions, 1)
print "Wanna go again?"
repeat = input()
input()
Upvotes: 1
Views: 321
Reputation: 56684
Here is a restructured version which should be easier to follow:
import random
import sys
if sys.hexversion < 0x3000000:
# Python 2.x
inp = raw_input
rng = xrange
else:
# Python 3.x
inp = input
rng = range
def get_yn(prompt):
while True:
res = inp(prompt).strip().lower()
if res in {'y','yes'}:
return True
elif res in {'n','no'}:
return False
def get_int(prompt):
while True:
try:
return int(inp(prompt))
except ValueError:
pass
def get_str(prompt):
while True:
res = inp(prompt).strip()
if res:
return res
rand = random.choice
actions = [
"{boy} must compliment {girl}'s hair",
"{girl} must admire {boy}'s muscles"
]
def main():
num_boys = get_int("How many boys are playing? ")
num_girls = get_int("How many girls are playing? ")
if num_boys < 1 or num_boys > 4:
print("Must be one to four boys.")
elif num_girls < 1 or num_girls > 4:
print("Must be one to four girls.")
else:
print("Let's get this party started!")
boys = [get_str("Name of boy {}: " .format(i)) for i in rng(1, num_boys+1) ]
girls = [get_str("Name of girl {}: ".format(i)) for i in rng(1, num_girls+1)]
while True:
act = rand(actions)
boy = rand(boys)
girl = rand(girls)
print(act.format(boy=boy, girl=girl))
if not get_yn("Want to continue? "):
break
if __name__=="__main__":
main()
Upvotes: 2
Reputation: 132098
In the interest of showing the OP how much more improved this could be (and even this could be improved but is good as an illustration), I took a couple minutes to simplify things:
while True:
try:
num_girls = raw_input("Number of girls? ")
num_girls = int(num_girls)
if num_girls > 4 or num_girls < 0:
raise Exception("Number of girls must be between 0 and 4")
if num_girls == 0:
print "Your party is going to suck"
break
except ValueError as e:
print "You must enter a number 0-4"
except Exception as e:
print e
girls = []
for i in range(num_girls):
girls.append(raw_input("Name of girl #%d: " % (i + 1)))
while True:
try:
num_boys = raw_input("Number of boys? ")
num_boys = int(num_boys)
if num_boys > 4 or num_boys < 0:
raise Exception("Number of boys must be between 1 and 4")
break
except ValueError as e:
print "You must enter a number 1-4"
except Exception as e:
print e
boys = []
for i in range(num_boys):
boys.append(raw_input("Name of boy #%d: " % (i + 1)))
Then you can just do your random sampling at the end.
Upvotes: 0
Reputation: 2054
WELCOME TO PROGRAMMING! I am so happy to hear you are exploring programming!
Read up on Don't Repeat Yourself (DRY)
And also on Object Oriented Programming (OOP) (In Python (and most languages?) "objects" are usually called: Classes
Upvotes: 0