I have recently been doing a gamedev class in my high school and we have been using Mac's terminal to run python coding to make a text based game. I think it's a version of python 2.7 and I have encountered an error
while Menuselect != "play":
MenuSelect = raw_input("Type 'quit' to exit - 'credits' for credits - Type 'play' to start\n")
MenuSelect = MenuSelect.lower()
if MenuSelect == "quit":
SystemExit(0)
elif MenuSelect == "credits":
print("Lmao, only Matt made this")
else:
print("You mistyped. 10/10")
print(": You wake up in a dark room, you don't know where you are or how you got here :")
When I try and run it and enter 'play' to stop the loop, it just continues the loop again. Anything wrong with it?
You are checking the variable name "Menuselect" with lowercase "s", but assigning the raw_input to a variable called "MenuSelect" with capital "S". Change the 'while' statement to
while MenuSelect != "play":
and it should work.
Related
I was wondering if someone here could help better explain For loops than my textbooks.
I'm writing a program in Python where the program askes a user if they're already an existing user. If they are an existing user, the program starts. If they're not an existing user, they're prompted to create a username and password.
I was wondering if someone could help explain how I could use a For Loop to loop the if statement incase a user enters some unexpected input like A or a number.
Thanks
Here's a code snippet of an if statement I have where it goes:
if newuser == "y":
print("Hello New User")
elif newuser == "n":
print("Hello Existing User")
else:
print("Please try again")
Try creating a while loop, and only break out of this if a condition is met.
while True:
newuser = input('enter y/n')
if newuser == "y":
print("Hello New User")
break
elif newuser == "n":
print("Hello Existing User")
break
else:
print("Please try again")
Whenever I select option 1 to play the game, the menu resets itself and starts from the beginning. It only does this once however and on the second time, choosing option 1 starts the game.
When I select any other option, it runs on the first attempt and does not repeat. Any help to stop this?
#Jake Eaton
#Menu
import time
from typing import type, type2
from main_game import Main_Game #Imports time, system and name Modules
from view_leaderboard import View_LB #Imports defined code with the names type, type 2, Main_Game and View_LB
from os import system, name
def clear():
if name =='nt': #Defines the clear command for the Terminal
_ = system('cls')
type("Welcome\n")
time.sleep(2)
type("Welcome to the only Text Adventure you will ever need to play in order to learn!\n")
time.sleep(2)
type("This Text Adventure covers the AQA A-level Computer Science Topic 4.6\n")
time.sleep(2)
type("Please remember that this is still in Beta and may never have a full 1.0 release\n")
time.sleep(2)
type("All releases and the source code can be found on Github using the link: https://github.com/Spud-Lord/Mock-NEA\n")
time.sleep(2)
type("To view the releases, just click on the releases tab on the Github Page\n")
time.sleep(2)
type("So let us begin!")
time.sleep(2)
type("Now then...")
time.sleep(2)
type("What do you want to do? Just type in the number of the option you want!")
time.sleep(2)
def Main_Menu(): #Defines all indented code as Main_Menu
menu = input("""
[1] - Start Game!
[2] - View Leaderboard
[3] - Exit Game
""") #The triple speech marks allow the Menu to go over multiple lines with only one code
if menu == "1":
clear() #Calls Clear Definition to clear Terminal Screen
Main_Game()
elif menu == "2":
print("")
View_LB() #Calls View_LB Definition to view leaderboard
time.sleep(2)
print("")
Main_Menu()
elif menu == "3":
exit() #Exits the game
else:
type("Please type in one of the numbers above...\n")
Main_Menu() #Loops back round if the user doesn't enter one of the numbers
Main_Menu() #Allows the Menu to loop
I did not reproduce your issue with the following code.
It seems you at least need to replace type(...) by print though!
The code needs to be cleaned up so that it becomes more readable. Put all your functions first then add the main part of the code (which starts at print("Welcome\n"))
#Jake Eaton
#Menu
import time
#from main_game import Main_Game #Imports time, system and name Modules
#from view_leaderboard import View_LB #Imports defined code with the names print, print 2, Main_Game and View_LB
from os import system, name
def clear():
if name =='nt': #Defines the clear command for the Terminal
_ = system('cls')
print("Welcome\n")
time.sleep(2)
print("Welcome to the only Text Adventure you will ever need to play in order to learn!\n")
time.sleep(2)
print("This Text Adventure covers the AQA A-level Computer Science Topic 4.6\n")
time.sleep(2)
print("Please remember that this is still in Beta and may never have a full 1.0 release\n")
time.sleep(2)
print("All releases and the source code can be found on Github using the link: https://github.com/Spud-Lord/Mock-NEA\n")
time.sleep(2)
print("To view the releases, just click on the releases tab on the Github Page\n")
time.sleep(2)
print("So let us begin!")
time.sleep(2)
print("Now then...")
time.sleep(2)
print("What do you want to do? Just print in the number of the option you want!")
time.sleep(2)
def Main_Game():
print ("Main game")
def View_LB():
print ("LB")
def Main_Menu(): #Defines all indented code as Main_Menu
menu = input("""
[1] - Start Game!
[2] - View Leaderboard
[3] - Exit Game
""") #The triple speech marks allow the Menu to go over multiple lines with only one code
if menu == "1":
clear() #Calls Clear Definition to clear Terminal Screen
Main_Game()
elif menu == "2":
print("")
View_LB() #Calls View_LB Definition to view leaderboard
time.sleep(2)
print("")
Main_Menu()
elif menu == "3":
exit() #Exits the game
else:
print("Please print in one of the numbers above...\n")
Main_Menu() #Loops back round if the user doesn't enter one of the numbers
Main_Menu()
The blessed Python library looks great for making console apps, but I'm having trouble with the keyboard functionality. In the code below, only every second keypress is detected. Why is this please?
I'm on Windows 10 with Python 3.8.
from blessed import Terminal
term = Terminal()
print(f"{term.home}{term.black_on_skyblue}{term.clear}")
with term.cbreak(), term.hidden_cursor():
while term.inkey() != 'q':
inp = term.inkey()
if inp.name == "KEY_LEFT":
print("You pressed left")
Move term.inkey() outside your inner while loop instead so it listens for keys first:
from blessed import Terminal
term = Terminal()
print(f"{term.home}{term.black_on_skyblue}{term.clear}")
with term.cbreak(), term.hidden_cursor():
inp = term.inkey()
while term.inkey() != 'q':
if inp.name == "KEY_LEFT":
print("You pressed left")
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
command = ""
while True :
command = input('>').lower()
if command =="start":
print('Your Car have Started ')
elif command == "stop":
print('Your Car have Stopped')
elif command == "help":
print('''
Start- To Start the car.
Stop- To Stop the car.
Quit- To Exit.
''')
elif command == "quit":
break
else:
print("Hey I don't understand your command")
break needs to be inside your loop, as the error says. You might have accidentally wrote it outside. Move the part below while by pressing tab.
Please check out the python formatting guide if it is unclear.
command = ""
while True :
command = input('>').lower()
if command =="start":
print('Your Car have Started ')
elif command == "stop":
print('Your Car have Stopped')
elif command == "help":
print('''
Start- To Start the car.
Stop- To Stop the car.
Quit- To Exit.
''')
elif command == "quit":
break
else:
print("Hey I don't understand your command")
I am currently working on a automated troubleshooter using Python. Below is a piece of my script which I need help with.
s1 = input("Is your phone freezing/stuttering? ")
if s1 == "Yes" or s1 == "yes":
print("Try deleting some apps and this might help with your problem.")
if s1 == "No" or s1 == "no":
def foo():
while True:
return False
So what I want to happen is my script to stop when the user types in YES and when the solution to the fix comes up. Is there a possible loop for that or something similar? Also if the user types in NO then I want the script to continue to the next question.
So one thing that you can do is utilize the sys.
So you can modify your program to look like the following:
import sys
s1 = input("Is your phone freezing or stuttering (yes/no)? ")
if s1.lower() == "yes":
print("Deleting some apps and this might help!")
elif s1.lower() == "no":
print("Your phone is working fine! Program is terminating.")
sys.exit(0) # this exits your program with exit code 0
The sys package is great for program control and also interacting with the interpreter. Please read more about it here.
If you don't want your program to exit and you just want to check that the user entered no, you could do something like:
import sys
s1 = input("Is your phone freezing or stuttering (yes/no)? ")
if s1.lower() == "yes":
print("Deleting some apps and this might help!")
elif s1.lower() == "no":
pass
else:
# if the user printed anything else besides yes or no
print("Your phone is working fine! Program is terminating.")
sys.exit(0) # this exits your program with exit code 0
Let me know if I can help in any other way!
EDIT
A comment by crickt_007 suggested that it might be helpful to repeat the input and continually query the user. You could wrap this whole function in a while loop then.
import sys
while True:
s1 = input("Is your phone freezing or stuttering (yes/no)? ")
if s1.lower() == "yes":
print("Deleting some apps and this might help!")
# solve their issue
elif s1.lower() == "no":
# supposedly move on to the rest of the problem
pass
else:
# if the user printed anything else besides yes or no
# maybe we want to just boot out of the program
print("An answer that is not yes or no has been specified. Program is terminating.")
sys.exit(0) # this exits your program with exit code 0
A while loop sounds like what you are looking for?
import sys
s1 = "no"
while s1.lower() != 'yes':
input("Is your phone freezing/stuttering? ")
if s1.lower() == 'yes':
print("Try deleting some apps and this might help with your problem.")
sys.exit(0)
elif s1.lower() == 'no':
print("something")
else:
print("invalid input")