Home work for Wednesday, May 13 !
PLease write a program that ineracts with the user and requires them to do something before the program ends.
It's OK if it does nothing more than ask for their name and tell them hello.
Below is a sample of such a program. I suggest that you copy it, paste it in, and run it before you read through it, otherwise you will know the answer to the question it asks. I also put this program in the examples list, at the top.
Pleaee email me (harry@baya.net) or call me (276-608-3060) if you have any questions or need any help.
############################### Sample program for homework #########################
#sample_homework_program.py
import random
import sys
import os
def user_entered(prompt_str):
#prompt the user to enter something:
print(prompt_str+" : ", end="")
#get value from user
in_str = sys.stdin.readline()
#to remove the trailing "\n", use rstrip()
in_str = in_str.rstrip()
#the trailing "\n" is now gone
return(in_str)
# end of user_entered function
def get_yes_no():
ans_good = "bad"
while (ans_good == "bad"):
user_ans = user_entered(" : yes or no")
if ( (user_ans == "yes") or (user_ans == "no") ):
ans_good = "good"
return(user_ans)
else:
print("\nYou entered >"+user_ans+"<")
print( "\nPlease enter yes or no, all lower case \n")
# end of while loop
#end of get_yes_no function
print ("Would you like to play a game? ",end = "")
yes_no_str = get_yes_no() #ask user to enter yes or no
# get_yes_no function returns yes or no in a tring
# This statement sets the variable yes_no_str
# equal to whatever is returned (i.e yea or no)
if (yes_no_str == "yes"):
print("\n Good! Here we go ! \n")
else:
print("\n Too bad, You have to play anyway.\n")
print("""\nTo win this game you must answer
the question correctly.""")
# NOTE: using tripple quotes, i.e """, permits
# entering text to be printed that
# is entered on more than one line
#
# To include a " inside a quoted line, use \"
# example: print ("This is a quote \" inside a quoted line")
winner = "no"
while (winner == "no"):
print(
"""\nWhat are the two kinds of loops we
have learned in Python. Enter two words, no commas.""")
user_ans = user_entered("\nEnter two words")
if ((user_ans == "while for") or (user_ans == "for while")):
print("\n Yae! You win! That is correct!")
winner = "yes"
else:
print("\n You entered : >"+user_ans+"<")
print("\n That is incorrect. Try again ! ")
#end of while loop
print("\n Because you have won, this game is over.\n\n")
print(
"""If this had been part of an adventure game,
the winner message would say something like\n
\"You are now permitted to enter LaLa Land !\"\n""")