I am currently making a text based rpg, and to solve the problem of case sensitivity during inputs, I used .upper to make it all uppercase. However, it seems to not work in my code. Can someone please help?
if weapon in normalswords or weapon in fireswords or weapon in airswords or weapon in grassSwords:
if weapon in normalswords:
print (normalswords[weapon])
while y== True:
variable= input("Equip? Yes or No")
variable.upper()
if variable== "YES":
print (weapon, "Equipped")
x= False
y=False
elif variable == "NO":
x= True
else:
print ("That is not a valid answer")
y=True
str.upper() returns a copy of the string converted to upper case. It does not modify the string inplace because strings are immutable in Python.
Try this:
variable = variable.upper()
Related
Pasting my code below.
I'm trying to create a text based game. In this portion I want my user to make a selection from the given items. If their selection is correct then they move out of the puzzle. If incorrect then they are to be prompted to try again.
I understand a list can be used to do this, but I feel less code can be used using a dictionary,and quality of life much better than:
challenge_1_response = input("> ").lower()
challenge_1_answers = ["magnifying glass", "beaker", "serrated knife", "callendar"]
if challenge_1_response == "magnifying glass":
print("If I look hard enough, I can see...")
elif challenge_1_response == "beaker":
print("If I smash that Beaker I can use the shard to cut loose")
elif challenge_1_response == "serrated knife":
print("You've cut loose")
elif challenge_1_response == "callendar":
print("Oh no. Has it really been that long?")
while challenge_1_response not in challenge_1_answers:
print("I can only choose what's in front of me")
challenge_1_response = input("> ").lower()
if challenge_1_response == "magnifying glass":
print("If I look hard enough, I can see...")
elif challenge_1_response == "beaker":
print("If I smash that Beaker I can use the shard to cut loose")
elif challenge_1_response == "serrated knife":
print("You've cut loose")
elif challenge_1_response == "callendar":
print("Oh no. Has it really been that long?")
Please see that this tidbit segment was just to be used as an example. The idea is that with a dictionary all possible keys along with their follow up message will be in a single variable as opposed to writing several lines of code if the same information was in a list. The other side to this is if I wanted to go back and make additions, or edit information, going back to a dictionary is easier.
I've gotten my code to
import sys
import time
import os
os.system("clear")
def puzzle_1():
challenge_1_scene = "The buckles are strapped tightly, it'll be tough prying myself loose. The lights above me are quite bright, it's painful to keep my eyes open. But...I can see there's a table next to me, and on it: [ Magnifying Glass, Beaker, Serrated Knife, Callendar ] Maybe I can use one of them to get out\n"
for character in challenge_1_scene:
sys.stdout.write(character)
sys.stdout.flush()
time.sleep(0.05)
challenge_1_response = input("> ").lower()
challenge_1_answers = {
"magnifying glass": "If I look hard enough, I can see...",
"beaker": "If I smash that Beaker I can use the shard to cut loose",
"serrated knife": "You've cut loose",
"callendar": "Oh no. Has it really been that long?",
}
if challenge_1_response in challenge_1_answers[challenge_1_response]:
print(challenge_1_answers[challenge_1_response])
while challenge_1_response not in challenge_1_answers[challenge_1_response]
print("I can only choose what's in front of me")
challenge_1_response = input("> ").lower()
if challenge_1_response in challenge_1_answers[challenge_1_response]:
print(challenge_1_answers[challenge_1_response])
puzzle_1()
Can this be done with a dictionary?
challenge_1_response = input("> ").lower()
challenge_1_answers = {
"magnifying glass": "If I look hard enough, I can see...",
"beaker": "If I smash that Beaker I can use the shard to cut loose",
"serrated knife": "You've cut loose",
"callendar": "Oh no. Has it really been that long?",
}
if challenge_1_response in challenge_1_answers:
print(challenge_1_answers[challenge_1_response])
while challenge_1_response not in challenge_1_answers:
print("I can only choose what's in front of me")
challenge_1_response = input("> ").lower()
if challenge_1_response in challenge_1_answers:
print(challenge_1_answers[challenge_1_response])
I've seriously been on this all day. I even looped as to the different attempts I was trying. Long story short, I took out the dictionaries index value and just kept the dictionary itself in the "in" statement. When getting to the print statement I can call the user inputted key for its respective value. During the while loop I need it to look for that same statement, otherwise go for a loop.
I swore I tried this before, but must have been doing something wrong. Anyway this works :)
I understand it needs to be touched up, but I was mostly looking for it to function the way I wanted to. Getting it right is a breeze after that.
The for loop is working eventhough the provided value is in the list
Tried to run the code in different IDEs. but code did not work in any of these environments
#Check whether the given car is in stock in showroom
carsInShowroom = ["baleno", "swift", "wagonr", "800", "s-cross", "alto", "dezire", "ciaz"]
print("Please enter a car of your choice sir:")
carCustomer = input()
carWanted = carCustomer.lower()
for i in carsInShowroom:
if i is carWanted:
print("Sir we do have the Car")
break
else:
print("Sorry Sir we do not currently have that model")
Only else block running. when I enter wagonr, the output says "Sorry Sir we
do not currently have that model"
Change this,
if i is carWanted: # `is` will return True, if
to
if i == carWanted.strip(): # strip for remove spaces
Why?
is is for reference equality.
== is for value equality.
*Note: Your input should be wagonr not wagon r
This code should be printing "I am unit Alpha 07" when the user says something like "what's your name", but for some reason the if statement never returns true. Please help!
import difflib
while True:
talk = input("Please say something > ")
temp = (difflib.get_close_matches(talk, ['What is your name?', 'Hello', 'peach', 'puppy'],1,0.2))
print(temp)
if temp == "['What is your name?']":
print("I am unit Alpha 07")
break
continue
input()
Here's a screenshot
Sorry if this is really stupid.
Since temp is a list and you want to check if the first element of that list is What is your name? then you can't just do it all as a string as in put it like "['What is your name?']" as you have done, you need to check the first element (index 0) and then compare this:
if temp[0] == "What is your name?":
...
And this will work. Good luck!
I have a problem with this code. It splits a string into a list and parses through it .I want to do something when I identify a word in the list. I have looked at IF statements and although it makes logical sense the code only produces the statement "screen problem advice" regardless of the sentence inputted. I am a bit stumped as to why i cant use current_word in a conditional statement. Is there something obviously wrong with this?
text=str(input("enter your problem"))
words = text.split()
for current_word in words:
print(current_word)
if current_word=="screen":
print("screen problem advice")
elif current_word=="power":
print("power_problem advice ")
elif current_word=="wifi":
print("connection_problems advice")
Any advice would be much appreciated.
If I run your code on my machine, it works like it should.
Some short pattern for if elif elif elif else is to use some dict() and use the .get()-method for lookups.
Some code like this ...
if word == "one":
variable = "11111"
elif word == "two":
variable = "22222"
elif word == "three":
variable = "33333"
else:
variable = "00000"
... can be written in some much shorter form:
variable = dict(
one="11111",
two="22222",
three="33333"
).get(word, "00000")
Back to your problem. Here is some sample.
I created a detect function which yields all detected advices.
In the main function, the advices are just printed out.
Please note the .lower() inside ISSUES.get(word.lower()), so it catches all variation of "wifi", "Wifi", "WiFi", ...
def detect(message):
ISSUES = dict(
wifi="network problem_advice",
power="power problem_advice",
screen="screen problem_advice"
)
for word in message.split():
issue = ISSUES.get(word.lower())
if issue:
yield issue
def main(message):
[print(issue) for issue in detect(message)]
if __name__ == '__main__':
main("The screen is very big!")
main("My power supply is working fine, thanks!")
main("Wifi reception is very good today!")
Further, I deliberately chose some weird examples to point you to some basic problem with your attempt to solve the problem.
A simple string matching won't be enough, as is produces false-positives in this case.
Try to think of some other approach.
I know that it is so stupid question but I'm stuck. How should I write this condition:
some_dic = { "single": single_player,
"s": single_player,
...
"m": multiplayer
}
mode_choice = ' '
while mode_choice not in some_dic:
mode_choice = input("Enter The Game Mode: ")
if some_dic(mode_choice) == single_player
...
I mean before the last line, of course. While executing a popup: "'dict' object is not callable" appears. I'm asking for a solution and a some explanation, if it is not a problem.
Best regards
Accessing elements from a dictionary should be done with square brackets:
some_dic[made_choice]
Nevertheless, there's a better pattern you can use to avoid checking the dictionary twice (you are doing it at mode_choice not in some_dic and some_dic[mode_choice] for the same text):
player = None
while player is None:
player = some_dict.get(input("Enter The Game Mode: "))
Like this, player will be either None or one valid output from your dictionary (eg.: single_player) and the loop will persist while it is None.