0
  d1a = input ("Do you want to: A) Approach the house. B) Approach the stable. [A/B]? : ")
        if d1a == "A":
            print ("You approach the cottage.")
        elif d1a == "B":
            print ("You approach the stables.")
        else 

I have only just started learning python and it is my first language, so I am just playing about with it. Is it possible for the else statement to ask for the input again and it to be saved as the same variable when neither A or B is typed in?

EDIT:

import time
import random
import sys
print ("You wake up to find yourself in a clearing off a forest, sounded by tall")
print (" trees on all sides with a path ahead of you")
d1 = input ("Do you want to : A) Walk down the path. B)Move your way through the trees? [A/B]: ")
if d1 == "A":
    print ("You begin to walk down the path.")
    print (" As a Sidenote, during this adventure 'dice' will be thrown and the success of your chosen action")
    print (" will be determined by the result.")
    r1 = random.randint(0.0,10.0)
    if 4 > r1 > 0.1:
        print (" Your groggyness from waking means you reach the edge of the forest after nightfall.")
        print (" You see that the path continues towards a small cottage, the lights are out and no smoke rises from the chimney.")
        print (" Away from the cottage is a stable, where you can see a horse standing with its head outside the door")
        d1a = input ("Do you want to: A) Approach the house. B) Approach the stable. [A/B]? : ")
        if d1a == "A":
            print ("You approach the cottage.")
        elif d1a == "B":
             print ("You approach the stables.")
        else :
peterh
  • 11,875
  • 18
  • 85
  • 108
Hailstone13
  • 11
  • 1
  • 1
  • 2

4 Answers4

3

Something like this, using your code, while loop has been annotated:

# gather the input
# "while" is the loop statement, checking the condition and executing the code in the body of loop while the condition holds true
# obviously, "while True" will execute its body forever until "break" statement executes or you press Ctrl+C on keyboard
while True:
    d1a = input ("Do you want to: A) Approach the house. B) Approach the stable. [A/B]? : ")
    # check if d1a is equal to one of the strings, specified in the list
    if d1a in ['A', 'B']:
        # if it was equal - break from the while loop
        break
# process the input
if d1a == "A": 
    print ("You approach the cottage.") 
elif d1a == "B": 
    print ("You approach the stables.")

The sample above is just an example of how to accomplish the thing. The while loop will keep asking the guy at the keyboard to enter 'A' or 'B'. Then you check your input.

In the real code you'll want to create function to capture the input and make all fancy checks.

Andy W
  • 2,082
  • 1
  • 13
  • 9
  • WoW, thank you for the incredibly fast response, could you please just run through what the steps of this codes does? Partly for understanding and partly so i can learn for the future, thanks :DD – Hailstone13 Jan 13 '14 at 00:28
  • I have added the comments to the code, hope that helps :) – Andy W Jan 13 '14 at 00:55
2

In some languages You would sometimes tend to use switch-case structures instead of long if-elif-else statements in order to improve readability. In Python, there is no switch-case statement, but You can map Your choices in a dictionary. Functions can be stored as variables too.

def opt_a():
  print("You approach the cottage.")

def opt_b():
  print("You approach the stables.")

def invalid_opt():
  print("Invalid choice")

options = {"A":["Approach the house",opt_a], "B":["Approach the stable",opt_b]}

for option in options:
  print(option+") "+options.get(option)[0])

choise = input("Please make Your choise: ")

val = options.get(choise)
if val is not None:
  action = val[1]
else:
  action = invalid_opt

action()
Martin Hansen
  • 597
  • 6
  • 5
1

No. Python is not an imperative language; although it does support impreative programming. (i.e: There is no GOTO in Python).

You should use either functions and loops.

Example:

while True:
    d1a = input ("Do you want to: A) Approach the house. B) Approach the stable. [A/B]? : ")
    if d1a == "A":
        print ("You approach the cottage.")
    elif d1a == "B":
        print ("You approach the stables.")
    elif dia == "Q":
        break

This will (very trivially) keep printing "Do you want to: A) Approach the house. B) Approach the stable. [A/B]?" and stop when you enter Q. I twould be up to you to continue this structure of code with more logic to suit your desired implementation.

Note: Writing in this style will get difficulty and complex very quickly and eventually you will want to split up your code into functions, modules, etc.

James Mills
  • 18,669
  • 3
  • 49
  • 62
-3
# this code below welcomes the user 
print ("hello and welcome to my python quiz,my name is brandon and i am the quiz master")

print ("please type your name")

your_name = input ()

your_name = str(your_name)

score = 0# this code set the veriable to zero
# this is the question
print ("Question 1")

print ("what is 50x10")
answer = input ()
answer =int(answer)

if answer == 500:
   print ("good work")
   score = score + 1

else:

    print ("better luck next time")

    score = score - 1


print ("Question 2")

print ("what is (5x10)-4x2")
answer = input ()
answer =int(answer)

if answer == 94:
   print ("good work")
   score = score + 1

else:

    print ("better luck next time")

    score = score - 1

print ("Question 3")

print ("what is 600000000x10")
answer = input ()
answer =int(answer)

if answer == 6000000000:
   print ("good work")
   score = score + 1

else:

    print ("better luck next time")

    score = score - 1

print ("Question 4")

print ("what is 600000000/10")
answer = input ()
answer =int(answer)

if answer == 60000000:
   print ("good work")
   score = score + 1

else:

    print ("try again")

    score = score - 1

print ("Question 5")

print ("what is 19x2-2")
answer = input ()
answer =int(answer)

if answer == 36:
   print ("good work")
   score = score + 1

else:

    print ("try again")

    score = score - 1

print ("Question 6")

print ("what is (8x4) x 9")
answer = input ()
answer =int(answer)

if answer == 288:
   print ("good work")
   score = score + 1

else:

    print ("try again")

    score = score - 1

print ("Question 7")

print ("what is 15 x4")
answer = input ()
answer =int(answer)

if answer == 60:
   print ("good work")
   score = score + 1

else:

    print ("try again")

    score = score - 1

print ("Question 8")

print ("what is 19 x9")
answer = input ()
answer =int(answer)

if answer == 171:
   print ("good work")
   score = score + 1

else:

    print ("try again")

    score = score - 1

print ("Question 9")

print ("what is 9 x9")
answer = input ()
answer =int(answer)

if answer == 81:
   print ("good work")
   score = score + 1

else:

    print ("try again")

    score = score - 1

print ("Question 10")

print ("what is 10 x10")
answer = input ()
answer =int(answer)

if answer == 171:
   print ("good work")
   score = score + 1

else:

    print ("try again")

    score = score - 1    


print ("Question 10")

print ("what is 10 x10")
answer = input ()
answer =int(answer)

if answer == 171:
   print ("good work")
   score = score + 1

else:

    print ("try again")

    score = score - 1

print("Question 11")

print ("6+8x2: \n\
1.28 \n\
2.14 \n\
3.38 \n\
4.34 \n"
answer =int (input (Menu))

if answer ==   1.:
       print ("well done")
elif answer == 2.:
       print ("better luck next time")
elif answer == 3.:
       print ("looser")
elif answer == 4.:
       print ("go back to primary school and learn how to add")

print("Question 12")

print ("6194+10x2: \n\
1.12409 \n\
2.124081 \n\
3.14321 \n\
4.12408 \n"
       answer =int(input (Menu))

if answer ==   1.:
       print ("well done")
elif answer == 2.:
       print ("better luck next time")
elif answer == 3.:
       print ("looser")
elif answer == 4.:
       print ("go back to primary school and learn how to add")





if score > 8:# this line tells the program if the user scored 2 points or over to print the line below

   print("amazing your score is" + str(score))# this code tells the user that they have done well if they have got 2or more points or over

elif score < 4:
    print ("good work your score is" + str(score))
else:
    print ("better look next time your scor is" + str(score))

print("well done your score is " +str (score))#this print the users score

print("thank you for playing in the python maths quiz")
filmor
  • 30,840
  • 6
  • 50
  • 48