Hello this is for my into to computer science final, using python and Tweepy. The function works until the while loop, where the input automatically goes to the else statement. I don't understand why this is happening, but I am assuming that it has to do with comparing the question variable with the string 'yes'
#Search for a user
def usersearch():
fp=open('UserSearch.txt','w')
user_search=input("Please enter the name of the user you would like to look up")
user = api.get_user(user_search)
fp.write(user_search)
fp.write("\n")
fp.write("User details:")
fp.write("\n")
print("User details:")
print("User name:" + " " + user.name)
fp.write("User name:" + " " + user.name)
fp.write("\n")
print("User description:" + " " + user.description)
fp.write("User description:" + " " + user.description)
fp.write("\n")
print("User location:" + " " + user.location)
fp.write("User location:" + " " + user.location)
fp.write("\n")
#Issue recalling function
while True:
question = input("Would you like to look up another user? (yes/no)").lower
if question == 'yes':
usersearch()
continue
elif question == 'no':
break
else:
print('Please enter either (yes/no)')
Related
I'm new to python and am trying to fix this and By defualt its running the else statment I don't understand and need help. I'm new so please dumb your answers down to my -5 iq
menu = "Baguette , Crossiant , Coffee , Tea , Cappuccino "
print(menu)
if "" > menu:
order = input("What Would you like? \n ")
amount = input("How many " + order + " would you like?")
print( name + " Your " + amount + " " + order + " will be ready soon!\n\n\n")
price = int(4)
#converts number into intenger to do math correctly (int)
total = price * int(amount)
#turns total into string intead of intenger to prevent error in terminal
print("Thank you, your total is: $" + str(total))
print("Your " + amount + " of " + order + " is ready!")
print("Please enjoy you " + order + "!")
print("Please come again!")
else:
#If not on menu stops running
print("Sorry Thats not on the menu")
quit()
I changed the if "" to on_menu and list the options but it didn't work, and I asked people on discord.
menu = ["Baguette" , "Crossiant" , "Coffee" , "Tea" , "Cappuccino "]
count = 0
for item in menu:
print(str(count) +" -> "+str(item))
count += 1
order = int(input("\n What Would you like? \n "))
if order > len(menu) or order < 0 or order == "" or not menu[order]:
print("Sorry Thats not on the menu")
quit()
orderCount = int(input("How many " + menu[order] + " would you like?"))
print("Your " + str(orderCount) + " " + menu[order] + " will be ready soon!\n\n\n")
amount = int(4) * int(orderCount)
print("Thank you, your total is: $" + str(amount))
print("Your " + str(amount) + " of " + str(menu[order]) + " is ready!")
print("Please enjoy you " + str(menu[order]) + "!")
print("Please come again!")
Currently you are not checking the user input, you are just checking for a False statement based on what you defined. You need to check if the user input is on the menu, else quit the program.
menu = "Baguette , Crossiant , Coffee , Tea , Cappuccino "
print(menu)
name = input("What your name? \n ")
order = input("What Would you like? \n ")
if not order.lower() in menu.lower(): # checking if user input is in the menu
#If not on menu stops running
print("Sorry Thats not on the menu")
quit()
amount = input("How many " + order + " would you like?")
print( name + " Your " + amount + " " + order + " will be ready soon!\n\n\n")
price = int(4)
#converts number into intenger to do math correctly (int)
total = price * int(amount)
#turns total into string intead of intenger to prevent error in terminal
print("Thank you, your total is: $" + str(total))
print("Your " + amount + " of " + order + " is ready!")
print("Please enjoy you " + order + "!")
print("Please come again!")
#input name
name = input("What is your name?\n")
#create menu
menu = "Baguette, Crossiant, Coffee, Tea and Cappuccino"
#display menu
print(menu)
#input order
order = input("What would you like?\n")
#if the order item is in the menu
if order in menu:
#input quantity
amount = input("How many " + order + "s would you like? ")
#display message
print( name + " Your " + amount + " " + order + " will be ready soon!\n\n\n")
#setting price of all items to 4
price = 4
#calculating total
total = price * int(amount)
#turns total into string intead of intenger to prevent error in terminal
print("Thank you, your total is: $" + str(total))
print("Your " + amount + " of " + order + " is ready!")
print("Please enjoy you " + order + "!")
print("Please come again!")
#if the item is not in menu
else:
#display message
print("Sorry Thats not on the menu")
Hope this helps! I have added the explanation as comments.
Beginner question:
How do I have my input code ignore certain strings of text?
What is your name human? I'm Fred
How old are you I'm Fred?
How do I ignore user input of the word "I'm"?
How old are you I'm Fred? I'm 25
Ahh, Fred, I'm 25 isn't that old.
How do I again ignore user input when dealing with integers instead?
This is the code I have so far:
name = input("What is your name human? ")
while True:
try:
age = int(float(input("How old are you " + name + "? ")))
if age >= 50:
print(str(age) + "! Wow " + name + ", you're older than most of the oldest computers!")
else:
print ("Ahh, " + name + ", " + str(age) + " isn't that old.")
break
except ValueError:
print("No, I asked how old you were... ")
print("Let us try again.")
You could use .replace('replace what','replace with') in order to acheive that.
In this scenario, you can use something like:
name = input("What is your name human? ").replace("I'm ","").title()
while True:
try:
age = int(input("How old are you " + name + "? ").replace("I'm ",""))
if age >= 50:
print(str(age) + "! Wow " + name + ", you're older than most of the oldest computers!")
else:
print ("Ahh, " + name + ", " + str(age) + " isn't that old.")
break
except ValueError:
print("No, I asked how old you were... ")
print("Let us try again.")
Similarly, you can use for the words you think the user might probably enter, this is one way of doing it.
Why not use the list of words that should be ignored or removed from the input, i suggest you the following solution.
toIgnore = ["I'm ", "my name is ", "i am ", "you can call me "]
name = input("What is your name human? ")
for word in toIgnore:
name = name.replace(word, "")
while True:
try:
age = int(float(input("How old are you " + name + "? ")))
if age >= 50:
print(str(age) + "! Wow " + name + ", you're older than most of the oldest computers!")
else:
print ("Ahh, " + name + ", " + str(age) + " isn't that old.")
break
except ValueError:
print("No, I asked how old you were... ")
print("Let us try again.")
The output is following
if opt == ("1"):
print("Please enter Menu Item Number and table number")
i = input("Menu Item: ")
**n** = raw_input("Table Number: ")
print( "\n" + "Order Details:" + "\n" + "\n" + "Menu Item: " + **items[n]** + "\n" + "Table Number: " + n + "\n")
Trying to use a variable to get a string from an array
I'm a beginner to python and I'm trying to make a text adventure game in python as a filler of time. I'm having a problem with one of my lines of code that I don't know how to fix. I'm getting the error:
TypeError: Can't convert 'int' object to str implicitly
and I don't know how to fix it. It's for a health counter I have got in the game that degrades health if you get hit. This is the code of my game so far:
# Imports
import sys
import os
# Main Vars
gmName = "TXT Adventure"
gmFileName = "txtAdventure"
gmVersion = "2.5.0"
gmSplit = "--------"
# Player Vars
plHealth = 100
plHealthO = 100 # Original Health
plName = "N/A"
# Character Vars
ch1 = "Tom"
ch2 = "Josh"
ch3 = "Demitrie"
# Enemy Vars - en<num>H = heath of enemy
en1 = "Sand Viper"
en1H = "5"
en2 = "Water Venom Moth"
en2H = "7"
en3 = "Giant Ant"
en3H = "10"
# Game
print("Finished Loading!")
print(gmSplit)
cont = input("Press Enter To Continue..")
print(gmSplit)
print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n") # Space from loading screen
print("Welcome to " + gmName + "!")
print("You are playing on version: " + gmVersion)
print(gmSplit)
plName = input("Who are you, son?\n--> ")
if plName == "":
exit = input("Invalid Name, Press enter to restart.. ")
os.startfile(gmName + '.py')
sys.exit(0)
if plName == " ":
exit = input("Invalid Name, Press enter to restart.. ")
os.startfile(gmName + '.py')
sys.exit(0)
cont = input("Press Enter to continue, " + plName + "!")
print(gmSplit)
print(ch1 + ": Well hello, " + plName + ". Nice to see you!")
cont = input("Press Enter to continue, " + plName + "!")
print(gmSplit)
option = input("What do you want to do?\n1 = Play\n2 = Exit\n--> ")
if option == "1":
cont = input("Press Enter to continue, " + plName + "!")
print(gmSplit)
plAge = input("What is your age, " + plName + "?" + "\n--> ")
print(gmSplit)
option2 = input("So you are " + plName + " and you are " + plAge + " years old?\n1 = True\n2 = False\n--> ")
if option2 == "1":
# Start of the game
print(gmSplit)
print("Well welcome to " + gmName + ", " + plName + "!")
print(gmSplit)
start = input("Press Enter To Start The Game..")
print(gmSplit)
print(ch1 + ": Hi there traveler, we are from far away lands!")
cont = input("Press Enter To Continue..")
print(gmSplit)
print(ch2 + ": Like the scally said, we' came from a long way away ay!")
cont = input("Press Enter To Continue..")
print(gmSplit)
print(ch1 + " + " + ch2 + ": I WILL KILL UUU!")
cont = input("Press Enter To Continue..")
print(gmSplit)
option = input(ch3 + ": Break it up you to! Don't you think, " + plName + "!\n1 = True\n2 = False\n--> ")
if option == "1":
print(ch3 + ": Well thank you, " + plName + ".")
print(ch1 + " + " + ch2 + ": Ugh...")
print(gmSplit)
else:
print(ch3 + ": Oh, i'm sorry, did I upset you..")
cont = input("Press Enter To Continue..")
print(gmSplit)
print(ch3 + ": Well anyway, they are still fighting..")
print(ch1 + " + " + ch2 + ": ARAGHHH!")
print(gmSplit)
cont = input("Press Enter To Continue..")
print(gmSplit)
print(ch1 + " + " + ch2 + " + " + ch3 + ": Okay lets start the adventure!")
cont = input("Press Enter To Continue..")
way = input(ch1 + ": Okay, " + plName + ". What way are we going?\n1 = North\n2 = South\n--> ")
if way == "1":
print(ch1 + ": Okay, lets go north!")
elif way == "2":
print(ch1 + ": Okay, lets go south!")
print(gmSplit)
cont = input("Press Enter To Continue..")
print(gmSplit)
print(ch3 + ": But I dont like going south, it's scary that way..")
print(gmSplit)
cont = input("Press Enter To Continue..")
print(gmSplit)
print(ch1 + " + " + ch2 + ": Shut up, " + ch3 + "!")
print(ch3 + ": Please don't go this way, " + plName + " :(")
option = input("1 = Continue South\n2 = Go North insted\n--> ")
if option == "1":
plHealth -= 5
print(gmSplit)
print(ch3 + ": Why, " + plName + ". Why did you do this!")
print(gmSplit)
print("*" + ch3 + " slaps you, your health goes from " + plHealthO + " to: " + plHealth + "!")
else:
print("You picked an invalid choice!")
exit = input("Press enter to re-do choice!")
print(gmSplit)
print("You have encounterd a wild: " + en1 + "! It has " + en1H + " health!")
option = input("What do you want to do?\n1 = Attack\n2 = Run\n--> ")
if option == "1":
en1H = "3"
plHealth -= 2
print("The " + en1 + "'s health has gone down to: " + en1H + "!")
cont = input("Press Enter To Continue..")
print(gmSplit)
print("The " + en1 + " attacked you!")
cont = input("Press Enter To Continue..")
print(gmSplit)
print("Your health was: " + plHealthO + "But now it has gone down to: " + plHealth + "!")
cont = input("Press Enter To Continue..")
print(gmSplit)
print(ch1 + " + " + ch2 + " Helped you and killed the beast!")
cont = input("Press Enter To Continue..")
print(gmSplit)
print("Okay, where do you want to go now?")
option = input("1 = Keep Heading Forward\n2 = Camp for the night\n--> ")
elif option == "2":
exit = input(en1)
else:
print("You chose an invalid choice!")
print(gmSplit)
restart = input("Press enter to restart..")
os.startfile(gmFileName + '.py')
sys.exit(0)
elif option2 == "2":
exit = input("Press enter to restart.. ")
os.startfile(gmFileName + '.py')
sys.exit(0)
else:
exit = input("Press enter to restart.. ")
os.startfile(gmFileName + '.py')
sys.exit(0)
elif option == "2":
exit = input("Press enter to exit " + gmName + ".. ")
os.startfile(gmFileName + '.py')
sys.exit(0)
else:
exit = input("Press enter to restart.. ")
os.startfile(gmFileName + '.py')
sys.exit(0)
The full error with my game goes like this:
Traceback (most recent call last):
File "H:\Phyton Programming\txtAdventure.py", line 112, in <module>
print("*" + ch3 + " slaps you, your health goes from " + plHealthO + " to: " + plHealth + "!")
TypeError: Can't convert 'int' object to str implicitly
I hope you can help me and sorry if i'm just not seeing something very obvious. Thanks!
plHealthO and plHealth are ints. To join them with a string you have to implicitly convert them to a strings.
print("*" + ch3 + " slaps you, your health goes from " + str(plHealthO) + " to: " + str(plHealth) + "!")
to convert int to string use use print ("text"+str(variable_name))
I've made a few programs in Python now, but I'm still pretty new. I've updated to 3.3, and most of my programs are broken. I've replaced all of the raw_inputs now with input, but this still isn't working, and I get no errors.
Could one of you fine programmers help?
a = 1
while a < 10:
StartQ = input("Would you like to Start the program, or Exit it?\n")
if StartQ == "Exit":
break
elif StartQ == "Start":
AMSoD = input("Would you like to Add, Multiply, Subtract or Divide?\nPlease enter A, M, S or D.\n")
if AMSoD == "A":
Add1 = input("Add this: ")
Add2 = input("By this: ")
AddAnswer = int(Add1) + int(Add2)
AAnswer = Add1 + " " + "+" + " " + Add2 + " " + "=",AddAnswer
print(AAnswer)
print("The answer is:"),AddAnswer
elif AMSoD == "M":
Mul1 = input("Multiply this: ")
Mul2 = input("By this: ")
MulAnswer = int(Mul1) * int(Mul2)
MAnswer = Mul1 + " " + "*" + " " + Mul2 + " " + "=",MulAnswer
print(MAnswer)
print("The answer is:"), (MulAnswer)
elif AMSoD == "S":
Sub1 = input("Subtract this: ")
Sub2 = input("From this: ")
SubAnswer = int(Sub2) - int(Sub1)
SAnswer = Sub2 + " " + "-" + " " + Sub1 + " " + "=",SubAnswer
print(SAnswer)
print("The answer is:"), (SubAnswer)
elif AMSoD == "D":
Div1 = input("Divide this: ")
Div2 = input("By this: ")
DivAnswer = int(Div1) / int(Div2)
DAnswer = Div1 + " " + "/" + " " + Div2 + " " + "=",DivAnswer
print(DAnswer)
print("The answer is:"), (DivAnswer)
DivQoR = input("Would you like to Quit or restart?\nAnswer Quit or Restart.\n")
if DivQoR == "Restart":
a = 1
elif DivQoR == "Quit":
DivQoRAyS = input("Are you sure you want to quit? Answer Yes or No.\n")
if DivQoRAyS == "Yes":
break
elif DivQoRAyS == "No":
a = 1
Put all items you want to print in the parenthesis of the print() function call:
print("The answer is:", AddAnswer)
and
print("The answer is:", MulAnswer)
etc.
Where you build your strings, it'd be easier to do so in the print() function. Instead of
AAnswer = Add1 + " " + "+" + " " + Add2 + " " + "=",AddAnswer
print(AAnswer)
(where you forgot to replace the last comma with +), do this:
print(Add1, '+', Add2, '=', AddAnswer)
and so on for the other options.