Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner question
#1
Hi there everybody. I'm a total beginner to Python, I only just started looking at it yesterday, so at the moment I'm basically just attempting to do some simple things like dice rolls, but I'm mostly wondering how to create questions, specifically multiple choice questions.

So my first attempt reads like this, I don't want to run before I can walk!

#Choices

import time

dir1="Forward"

dir1=input("Which way do you want to move?")

if dir1=="":
    print("You have to move!")
    (time.sleep(1))
    print("-----")
    dir1=input("Which way do you want to move?")

while dir1=="Forward":
    print("CHARGE")
So I was thinking that the question "Which way do you want to move?" would keep on repeating as long as no answer was given, but it only repeats itself once, and then the program just ends if done again. My main question is how would I get the question to keep on looping? Also, when I do give the answer "Forward", "CHARGE" just becomes spam (at tremendous speed, too!), but I think I know why that is.

Thanks very much for reading guys, like I said I am very new to this, but any words of wisdom are most welcome.
Reply
#2
You have the right idea with a 'while' loop, but you want your code to repeat until they give an acceptable answer. If you're only looking for "Forward", your code might look like this:
while not dir1 == "Forward":
  dir1 = input("Which way do you want to move?")
This code will repeat until the user enters "Forward"
Reply
#3
When you want something to happen more than once, then you want a loop.  In this case, a while loop is probably right.  Something like
while True:
    direction = input("Where to, boss? ").lower()
    if not direction:
        print("You can't go nowhere.")
    elif direction == "forward":
        print("CHARGE!")
Reply
#4
(Nov-03-2017, 06:46 PM)Lux Wrote: You have the right idea with a 'while' loop, but you want your code to repeat until they give an acceptable answer. If you're only looking for "Forward", your code might look like this:
while not dir1 == "Forward":
  dir1 = input("Which way do you want to move?")
This code will repeat until the user enters "Forward"

(Nov-03-2017, 06:48 PM)nilamo Wrote: When you want something to happen more than once, then you want a loop.  In this case, a while loop is probably right.  Something like
whlie True:
    direction = input("Where to, boss? ").lower()
    if not direction:
        print("You can't go nowhere.")
    elif direction == "forward":
        print("CHARGE!")

Ah interesting, that makes sense, thanks very much to you both for the swift responses. I mean I want to eventually get to the point where multiple different answers can be given to a single question, like if you were writing an adventure story for example, but that's definitely something I will experiment with in the meantime. Just baby steps, at the moment.
Reply
#5
...I misspelt while.  Whoops.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A simple "If...Else" question from a beginner Serena2022 6 1,638 Jul-11-2022, 05:59 AM
Last Post: Serena2022
Question Beginner Boolean question [Guessing game] TKB 4 2,226 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  Beginner question NameError amazing_python 6 2,379 Aug-13-2021, 07:28 AM
Last Post: amazing_python
  Beginner question - storing values cybertron2 4 3,137 Mar-09-2021, 04:21 AM
Last Post: deanhystad
  beginner question about lists and functions sudonym3 5 2,666 Oct-17-2020, 12:31 AM
Last Post: perfringo
  beginner question ___ 1 1,704 Jul-12-2020, 08:12 AM
Last Post: Gribouillis
  Beginner question: lxml's findall in an xml namespace aecklers 0 2,864 Jan-22-2020, 10:53 AM
Last Post: aecklers
  Super easy beginner question AkulaLA 3 3,173 Nov-07-2019, 03:42 AM
Last Post: Larz60+
  Basic Beginner question NHeav 4 2,707 Sep-13-2019, 11:43 AM
Last Post: NHeav
  Beginner Question - Esaping the Escape Character correctly? Bramen 4 2,653 Aug-27-2019, 02:38 PM
Last Post: Bramen

Forum Jump:

User Panel Messages

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