Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Text Based Game
#1
I am a complete noob and old to boot, I have created some code that will give the user 2 chances to get the correct answer, if they are wrong 2 times, a HINT will print to the terminal due to a nested while loop, I would like to give them 2 more chances, then give them the ANSWER, I cannot get an additional nested while loop to work, please help an old guy. Maybe another nested while loop is not what is required?? Wrong is a global code declared at the top of the game, along with Correct (not used in this type of multiple answer question 'a' thru 'g'). Wdo 10, python 3.

    wrngCnt = 0

    q2a = input('Construct the minimal BOILER PLATE HTML Template answer NOW: \n\n')
    if q2a == '<!DOCTYPE html>':
        time.sleep(1)
    while q2a != '<!DOCTYPE html>':
        print(wrong)
        q2a = input()                   #Waiting for the user to input the correct answer again
        while wrngCnt < 1:
            wrngCnt = wrngCnt + 1
            print('')
            print('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@')
            print('HINT: This line of code requires the HTML5 tag, no indent.)
            print('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@')
            print('')
    q2b()
Hope the tags are working, this is the first question 'a' thru 'g' to allow the user to type into the terminal the minimal boiler plate that I previously taught in a tutorial within the game, if we can make 'a' work, I can get the next 1000 questions to work, thanks in advance for any assistance.
Reply
#2
Typically, when I want to keep asking the same question, I use this format:

while True:
    answer = input('Question? ')
    if answer == correct:
        break
    print('Wrong.')
You want to add the wrinkle of four chances, with a hint after the second chance. I think changing it to a for loop would work fine.

for guess in range(4):
    answer = input('Question? ')
    if answer == correct:
        break
    if guess == 1:
        print('Hint.')
    else:
        print('Wrong.')
else:
    print('Correct answer.')
The else clause for a loop triggers if the loop exits without a break statement. Since the only break statement happens with a correct answer, exiting without a break means they never got the correct answer, so you show it to them.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
I inserted my prompt text with spacing as needed. Since this type of question requires multiple lines of code without a space between lines I removed the last else statement from the algorithm.

I tested it fully with wrong answers on the first thru the forth iteration and this is my final output, fully tested and working. Only needed minor adjustments based on my requirements.

This is q2a, the first of seven segments to answer without further prompt, line after line without any blank spacing between lines of code. For multi segmented lines of output, I have to create a code snippet like this in three slightly different variations based on if the segment is the very first segment, a mid segment or the final segment.

    for guess in range(4):
        q2a = input('Construct the minimal BOILER PLATE HTML Template answer NOW: \n\n')
        if q2a == '<!DOCTYPE html>':
            break
        if guess == 1:                  #if user guesses wrong answer on the second try, a hint is provided
            print('')
            print('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@')
            print('HINT: This line of code requires the HTML5 tag, no indent, then hit the ENTER KEY and code the next line')
            print('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@')
            print('')
        elif guess == 3:                #if user guesses wrong answer on the forth try, the answer is provided
            print('')
            print('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@')
            print('ANSWER:     <!DOCTYPE html>     Type in this answer NOW, then hit the ENTER KEY and code the next line')
            print('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@')
            print('')
            q2a = input()               #the cursor is waiting for user to type in the answer given to them
        else:
            print(wrong)                #a global wrong text message is printed to screen
#    else:
#        print('Correct answer.')       #remove or move this based on single or multiple answer questions, NO MULTI
    q2b()
Thank you Ichabod801 for the awesome code, does this forum give me the option to mark as DONE or give you a rating?
Reply
#4
You may be able to modify the title of the thread to add "[SOLVED]" to it, but I think you need five posts to do that. I wouldn't worry about it, it is not done regularly around here.

If you want to give me a rating, you can give me reputation (the 'rep' button with the star), but it appears that you have already done that.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Text-Based RPG - After selecting correct decision it also prints others code KimJiwoo1 1 987 Apr-26-2023, 08:30 PM
Last Post: deanhystad
  Complete beginnner making a text based adventure Mishmaccles 2 2,670 Jul-07-2021, 05:00 PM
Last Post: BashBedlam
  Adding an inventory and a combat system to a text based adventure game detkitten 2 6,869 Dec-17-2019, 03:40 AM
Last Post: detkitten
  Using classes for room movement (text game) Lawr3y 3 6,566 Aug-20-2019, 12:40 AM
Last Post: Lawr3y
  Simple Game - Choice Based theor 3 2,917 May-10-2019, 08:41 AM
Last Post: beLIEve
  Text Game Testing/Dev PKMindBlow 21 10,589 Feb-05-2018, 11:04 PM
Last Post: PKMindBlow

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020