I've started coding about 3 weeks ago, just for fun and maybe practical uses later. My GF dared me to make a PGM that would replace this dice game where you roll two dice, one with body parts and one with actions, then resulting in stuff like 'kiss, lips'.
All I know is off of YouTube and sites such as Stack Overflow. I would appreciate if I could get tips and constructive criticism on how to code more efficient, clear, correct and maybe more 'pythonic'.
A bunch of notes are in there to clarify what I'm doing. As far as I've tested it is working correctly. As you will see there is a load/save function intended to store the game data to a text file.
def enterplayers():
# Set players for the game
global player1
player1 = input('Enter player 1 name: ')
global player2
player2 = input('Enter player 2 name: ')
global playerlist
playerlist = [player1, player2]
global player
player = cycle(playerlist)
def enteractions():
# Add actions to existing (or empty) list
print("Add ACTIONS and enter 'q' when done")
while True:
nextitem = input("Add: ")
if nextitem == 'q':
break
elif nextitem == '':
continue
else:
nextitem = nextitem.lower()
if ' ' in nextitem:
nextitem = nextitem.replace(' ', '_')
actionlist.append(nextitem)
def enterbp():
# Add body parts to existing (or empty) list
print("Add BODY PARTS and enter 'q' when done")
while True:
nextitem = input("Add: ")
if nextitem == 'q':
break
elif nextitem == '':
continue
else:
nextitem = nextitem.lower()
if ' ' in nextitem:
nextitem = nextitem.replace(' ', '_')
bplist.append(nextitem)
def loaddata():
#empty them out in case something there
global bplist
bplist = []
global actionlist
actionlist = []
#Load each one in its correct list and players in each variable
while True:
loadfile = input('Enter file name to LOAD (including ".txt"): ')
print()
if loadfile == 'q':
return # It was told to me that using this is baaadddd baaaaddddd; is that true?
try:
with open(loadfile,'rt') as file:
for line in file:
line = line.split()
if 'action:' in line:
line.remove('action:')
newitem = ''
newitem = newitem.join(line)
actionlist.append(newitem)
if 'bodypart:' in line:
line.remove('bodypart:')
newitem = ''
newitem = newitem.join(line)
bplist.append(newitem)
if 'player1:' in line:
line.remove('player1:')
newitem = ''
newitem = newitem.join(line)
global player1
player1 = newitem
if 'player2:' in line:
line.remove('player2:')
newitem = ''
newitem = newitem.join(line)
global player2
player2 = newitem
break
except FileNotFoundError:
print("That didn't work out! Probably file does not exist or wrong file location.")
print("Try again, or enter 'q' to return to main menu.")
print()
#Tell user what is loaded
print('Loaded players:')
print('-', player1)
print('-', player2)
print()
print('Loaded actions:')
for item in actionlist:
print('-', item)
print()
print('Loaded body parts:')
for item in bplist:
print('-', item)
print()
print('Loading complete')
print()
# Set lists to cycle through (since enterplayers() was not used)
global playerlist
playerlist = [player1, player2]
global player
player = cycle(playerlist)
def savedata():
print("Save including '.txt', any existing file will be overwritten!")
print()
while True:
savename = input('SAVE to what file? ')
if '.txt' in savename:
break
print("Don't forget .txt!")
print()
with open(savename, 'wt') as file:
file.write('player1: ' + player1 + '\n')
file.write('player2: ' + player2 + '\n')
for item in actionlist:
file.write('action: ' + item + '\n')
for item in bplist:
file.write('bodypart: ' + item + '\n')
print()
print('Successfully saved the following data to file', savename + ':')
print('Player 1:', player1)
print('Player 2:', player2)
print()
for item in actionlist:
print('Action:', item)
print()
for item in bplist:
print('Body part:', item)
print()
def pickfirstplayer():
# Check if user wants to pick randomly
while True:
print()
randomplayer = input('Pick first player randomly? ')
if randomplayer in ('no','yes'):
break
print('Please enter yes or no')
print()
# Pick randomly
if randomplayer == 'yes':
topickfrom = [player1,player2]
nextplayer = random.choice(topickfrom)
nextplayercheck = nextplayer
print('The first player is ' + nextplayer + '!')
# Here make sure that the firt playthrough is correct and not
# player1 or player2 do something to himself
nextplayer = next(player)
if nextplayer != nextplayercheck:
nextplayer = next(player)
# Pick manually
elif randomplayer == 'no':
check = 0
nextplayer = ''
while check == 0:
var = input('who is first player? ')
if var not in playerlist:
print('That player is not playing!')
continue
while var != nextplayer:
nextplayer = next(player)
check = 1
print()
def menu():
print()
print('- - - - - - - - - - - -')
print(' MAIN MENU')
print()
print('1. Start game')
print()
print('2. Enter player names')
print('3. Add actions')
print('4. Add body parts')
print()
print('6. Load data')
print('7. Save data')
print()
print('8. Print instructions')
print('9. Quit ')
print('- - - - - - - - - - - -')
print()
def instructions():
print('Before starting the game you must set the following:')
print('\t\t- Names of the players')
print('\t\t- Actions (touch, kiss, etc.)')
print('\t\t- Body parts')
print()
print('You can also just load a file with game data.')
print()
print('You do not need to capitalize, this is done automatically.')
print()
print('When this is done you can play the game!')
print()
print('Notice that when you use the load function,')
print(' all previously loaded data will be removed.')
print()
actionlist = []
bplist = []
import random
import time
import sys
from itertools import cycle
## 'actual code' starts here
while True:
menu()
try:
menuchoice = int(input('Enter choice: '))
except ValueError:
print('Please enter a number!')
continue
print()
if menuchoice == 1:
print('- - - - - - - -')
print('STARTING GAME!')
pickfirstplayer()
while True:
# Set action and place randomly
nextaction = random.choice(actionlist)
nextplace = random.choice(bplist)
# Print it
print('Okay', nextplayer + ',')
time.sleep(1)
nextplayer = next(player)
print()
print(nextaction + ' ', end="", flush=True)
time.sleep(1)
print(nextplayer+"'s ", end="", flush=True)
time.sleep(2)
print(nextplace + '!')
time.sleep(5)
print()
print()
anotherone = input('Press enter for another, type anything to quit ')
if anotherone == '':
print()
continue
else:
print('Returning to main menu')
print()
break
elif menuchoice == 2:
enterplayers()
elif menuchoice == 3:
enteractions()
elif menuchoice == 4:
enterbp()
elif menuchoice == 6:
loaddata()
elif menuchoice == 7:
savedata()
elif menuchoice == 8:
instructions()
elif menuchoice == 9:
quit()
else:
print('Please enter a valid number!')
print()