Update functions.py

Added PyWebIO inpout text and floats
This commit is contained in:
KansaiGaijin
2024-08-11 01:31:34 +12:00
committed by GitHub
parent 14732cf007
commit d0ab3f44d4

View File

@@ -10,39 +10,40 @@ def wait():
input("Press enter to continue...") input("Press enter to continue...")
from pywebio.input import input, FLOAT, TEXT
from pywebio.output import put_text
from pywebio.session import run_js
def startGame(): def startGame():
print("Welcome, stranger.") put_text("Welcome, stranger.")
start = input("Would you like to create your own character or use pre-generated stats?" start = input("Would you like to create your own character or use pre-generated stats?"
"\n 1) Create a new character\n 2) Pre-generated\n >> ").title() "\n 1) Create a new character\n 2) Pre-generated\n >> ", type=TEXT).title()
while True: while True:
if start in ("2", "Pregen"): if start in ("2", "Pregen"):
print("Please be prepared to enter a name, gender, age, race, and class, from those available in the game." put_text("Please be prepared to enter a name, gender, age, race, and class, from those available in the game."
"\nIf you are unsure what the options are, please go back and create a new character.") "\nIf you are unsure what the options are, please go back and create a new character.")
create = input("Do you wish to continue? Y/N\n >> ").lower() create = input("Do you wish to continue? Y/N\n >> ", type=TEXT).lower()
if create in ("y", "yes"): if create in ("y", "yes"):
name = input("Name: ").title() name = input("Name: ", type=TEXT).title()
gender = input("Gender: ").title() gender = input("Gender: ", type=TEXT).title()
age = int(input("Age: ")) age = int(input("Age: ", type=FLOAT))
race = input("Race: ").title() race = input("Race: ", type=TEXT).title()
job = input("Job: ").title() job = input("Job: ", type=TEXT).title()
return name, gender, age, race, job return name, gender, age, race, job
elif create in ("n", "no"): elif create in ("n", "no"):
start = "1" start = "1"
elif start in ("1", "create"): elif start in ("1", "Create"):
while True: # Global check to see if Name, Age, and Gender are correct while True: # Global check to see if Name, Age, and Gender are correct
# Initialise age
age = None
# Name # Name
name = input(f'\nWhat is your name?\n >> ').title() name = input('What is your name?\n >> ', type=TEXT).title()
while len(name) < 2: while len(name) < 2:
name = input(f'Input name was too short. Try again. \nWhat is your name?\n >> ').title() name = input('Input name was too short. Try again.\nWhat is your name?\n >> ', type=TEXT).title()
# Gender # Gender
while True: while True:
gender = None
gender_select = input("What is your gender?\n" gender_select = input("What is your gender?\n"
"1) Male\n2) Female\n3) Other\n >> ").lower() "1) Male\n2) Female\n3) Other\n >> ", type=TEXT).lower()
if gender_select not in ("male", "female", "other", "1", "2", "3"): if gender_select not in ("male", "female", "other", "1", "2", "3"):
print("Sorry I didn't recognise that gender. Please try again.") put_text("Sorry I didn't recognise that gender. Please try again.")
continue continue
else: else:
break break
@@ -55,46 +56,45 @@ def startGame():
while True: while True:
# Age # Age
try: try:
age = int(input(f'How old are you?\n >> ')) age = int(input('How old are you?\n >> ', type=FLOAT))
break break
except ValueError: except ValueError:
print("Sorry I didn't recognise that age. Please type in a whole number.") put_text("Sorry I didn't recognise that age. Please type in a whole number.")
continue continue
# Double check responses # Double check responses
correct = input(f"Hello, {name}. You are a {age} year old {gender}." correct = input(f"Hello, {name}. You are a {age} year old {gender}.\nIs this correct? Y/N\n >> ", type=TEXT).lower()
f"\nIs this correct? Y/N\n >> ").lower()
if correct in ['y', 'yes']: if correct in ['y', 'yes']:
pass break
elif correct in ['n', 'no']: elif correct in ['n', 'no']:
continue continue
else: else:
print("Sorry, I didn't catch that.\n") put_text("Sorry, I didn't catch that.\n")
correct = input(f"You are a {age} year old {gender}. \nIs this correct? Y/N\n >> ").lower() correct = input(f"You are a {age} year old {gender}. \nIs this correct? Y/N\n >> ", type=TEXT).lower()
if correct in ['y', 'yes']: if correct in ['y', 'yes']:
break break
elif correct in ['n', 'no']: elif correct in ['n', 'no']:
continue continue
break break
while True: # Global check to see if Race and Job are correct. while True: # Global check to see if Race and Job are correct
# Race # Race
while True: while True:
race = input("Please select a race to learn more about it: Elf, Dwarf, or Human.\n >> ").title() race = input("Please select a race to learn more about it: Elf, Dwarf, or Human.\n >> ", type=TEXT).title()
if race not in ("Elf", "Dwarf", "Human", None): if race not in ("Elf", "Dwarf", "Human", None):
race = input("Sorry I didn't recognise that race. " race = input("Sorry I didn't recognise that race. Please select 'Elf', 'Dwarf', or 'Human'.\n >> ", type=TEXT).title()
"Please select 'Elf', 'Dwarf', or 'Human'.\n >> ").title()
elif race is None: elif race is None:
race = input("Please select a race to learn more about it: Elf, Dwarf, or Human.\n >> ").title() race = input("Please select a race to learn more about it: Elf, Dwarf, or Human.\n >> ", type=TEXT).title()
continue continue
else: else:
# Replace these prints with appropriate explanations for the web interface
if race == "Elf": if race == "Elf":
print(Elf) put_text("Information about Elves...")
elif race == "Dwarf": elif race == "Dwarf":
print(Dwarf) put_text("Information about Dwarves...")
elif race == "Human": elif race == "Human":
print(Human) put_text("Information about Humans...")
print('Would you like to proceed with this race or view another?') put_text('Would you like to proceed with this race or view another?')
proceed = input(f'1) Proceed\n2) View another race\n >> ').lower() proceed = input('1) Proceed\n2) View another race\n >> ', type=TEXT).lower()
if proceed in ('1', 'proceed'): if proceed in ('1', 'proceed'):
break break
elif proceed in ('2', 'view', 'view another', 'view another race'): elif proceed in ('2', 'view', 'view another', 'view another race'):
@@ -102,24 +102,22 @@ def startGame():
continue continue
# Job # Job
while True: while True:
job = input("Please select a job to learn more about it: Barbarian, Cleric, or Wizard." job = input("Please select a job to learn more about it: Barbarian, Cleric, or Wizard.\n >> ", type=TEXT).title()
"\n >> ").title()
if job not in ("Barbarian", "Cleric", "Wizard", None): if job not in ("Barbarian", "Cleric", "Wizard", None):
job = input("Sorry I didn't recognise that job. " job = input("Sorry I didn't recognise that job. Please select 'Barbarian', 'Cleric', or 'Wizard'.\n >> ", type=TEXT).title()
"Please select 'Barbarian', 'Cleric', or 'Wizard'.\n >> ").title()
elif job is None: elif job is None:
job = input("Please select a job to learn more about it: Barbarian, Cleric, or Wizard." job = input("Please select a job to learn more about it: Barbarian, Cleric, or Wizard.\n >> ", type=TEXT).title()
"\n >> ").title()
continue continue
else: else:
# Replace these prints with appropriate explanations for the web interface
if job == "Barbarian": if job == "Barbarian":
print(Barbarian) put_text("Information about Barbarians...")
elif job == "Cleric": elif job == "Cleric":
print(Cleric) put_text("Information about Clerics...")
elif job == "Wizard": elif job == "Wizard":
print(Wizard) put_text("Information about Wizards...")
print('Would you like to proceed with this job or view another?') put_text('Would you like to proceed with this job or view another?')
proceed = input(f'1) Proceed\n2) View another job\n >> ').lower() proceed = input('1) Proceed\n2) View another job\n >> ', type=TEXT).lower()
if proceed in ('1', 'proceed'): if proceed in ('1', 'proceed'):
break break
elif proceed in ('2', 'view', 'view another', 'view another job'): elif proceed in ('2', 'view', 'view another', 'view another job'):
@@ -127,13 +125,13 @@ def startGame():
continue continue
while True: while True:
# Final check # Final check
correct = input(f"{name}, you are a {race} {job}.\nIs this correct? Y/N\n >> ").lower() correct = input(f"{name}, you are a {race} {job}.\nIs this correct? Y/N\n >> ", type=TEXT).lower()
if correct in ['y', 'yes']: if correct in ['y', 'yes']:
break break
elif correct in ['n', 'no']: elif correct in ['n', 'no']:
continue continue
else: else:
print("Sorry, I didn't catch that. Please try again.\n") put_text("Sorry, I didn't catch that. Please try again.\n")
continue continue
return name, gender, age, race, job return name, gender, age, race, job