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!
Related
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
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()
Okay so I'm very new to the programming world and have written a few really basic programs, hence why my code is a bit messy. So the problem i have been given is a teacher needs help with asigning tasks to her students. She randomly gives the students their numbers. Students then enter their number into the program and then the program tells them if they have drawn the short straw or not. The program must be able to be run depending on how many students are in the class and this is where i am stuck, I can't find a way to run the program depending on how many students are in the class. Here is my code
import random
print("Welcome to the Short Straw Game")
print("This first part is for teachers only")
print("")
numstudents = int(input("How many students are in your class: "))
task = input("What is the task: ")
num1 = random.randint(0, numstudents)
count = numstudents
print("The part is for students") #Trying to get this part to run again depending on number of students in the class
studentname = input("What is your name:")
studentnumber = int(input("What is the number your teacher gave to you: "))
if studentnumber == num1:
print("Sorry", studentname, "but you drew the short straw and you have to", task,)
else:
print("Congratulations", studentname, "You didn't draw the short straw")
count -= 1
print("There is {0} starws left to go, good luck next student".format(count))
Read up on for loops.
Simply put, wrap the rest of your code in one:
# ...
print("The part is for students")
for i in range(numstudents):
studentname = input("What is your name:")
#...
Also, welcome to the world of programming! :) Good luck and have fun!
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.
Here is my code:
def collectData():
print ("Please use correct spelling\n")
print("Choose what game type this is")
getGameType = input(" FFA: 1\n 1v1: 2\n 2v2: 3\n 3v3: 4\n 4v4: 5\n")
if getGameType == "1":
FFAPlayerList = []
getFFAMaxLen = (int)(input("How Many players were in this FFA?: "))
print("Please enter playername as well as placing, seperated by a comma\n(playerName,placing) One player per entry. \n")
while len(FFAPlayerList) < getFFAMaxLen:
getFFAPlayers = input("format: playerName,Placing::: ").split(',')
FFAPlayerList.append(getFFAPlayers)
with open("C:\\Users\\Twinklenugget\\Documents\\NQGroup\\Databases\\NQMasterDatabase.csv", 'r') as ffaDatabaseExtract:
reader = csv.reader(ffaDatabaseExtract)
ffaDatabaseList = list(reader)
FFAPlayerList.append(ffaDatabaseList)
print (FFAPlayerList)
collectData()
Forgive the formatting, it's actually all correct. I am relativly new to python and coding in general. My goal is to be able to take the values stored in FFAPlayerList (the player names) and use those strings to look for the same strings in ffaDatabaseList. After it finds the same strings from ffaDatabaseList I then need to take values from the second column from ffaDatabaseList and append them into FFAPlayerList. How exactly would I go about doing that? Even a pointer in the right direction would be great.