How can I choose a random conversation output in Python? - python

I'm trying to get my script to choose between three options of conversation, labeled petconvo1, petconvo2, or petconvo3. The program asks what pet the user has and then picks a random thing to say about them. Based on what tidbit the script picks, I want to continue the conversation there. My problem is that it just stops and doesn't keep going with the conversation. What's the best way to do this?
My code is below.
#imports the random module
import random
#Asks what pet the user has
pet = input("What kind of pet do you have? ")
#Sets the input to lowercase
petlower = pet.lower()
#Sets tidbits of conversation about the pet
petconvo1 = ("Oh, I've heard that " + petlower + " is a good companion to have.")
petconvo2 = ("Oh, really? I had " + petlower + " when I was growing up.")
petconvo3 = ("Does it cost a lot to keep " + petlower + " fed?")
#Turns the tidbits into a list
petconvolist = [petconvo1, petconvo2, petconvo3]
#Compliments pet
print(random.choice(petconvolist))
#Continues the conversation
if petconvolist == petconvo1:
time.sleep(1)
print("Wouldn't you agree?")
else:
if petconvolist == petconvo2:
time.sleep(1)
print("I think it's important for children to grow up with pets.")
else:
if petconvolist == petconvo3:
time.sleep(1)
foodbudget=input("Does it cost a lot to feed " + petlower + "?")
time.sleep(.5)
else: pass
The script stops after this bit:
print(random.choice(petconvolist))
Thanks in advance!

hmm first in the if statements you should use the elif statement. Afterwards you should use set the random selection to a var in order to check it. This code works for me.
#imports the random module
import random
#Asks what pet the user has
pet = input("What kind of pet do you have? ")
#Sets the input to lowercase
petlower = pet.lower()
#Sets tidbits of conversation about the pet
petconvo1 = ("Oh, I've heard that " + petlower + " is a good companion to have.")
petconvo2 = ("Oh, really? I had " + petlower + " when I was growing up.")
petconvo3 = ("Does it cost a lot to keep " + petlower + " fed?")
#Turns the tidbits into a list
petconvolist = [petconvo1, petconvo2, petconvo3]
#Compliments pet
convo = random.choice(petconvolist)
print(convo)
#Continues the conversation
if convo == petconvo1:
time.sleep(1)
print("Wouldn't you agree?")
elif convo == petconvo2:
time.sleep(1)
print("I think it's important for children to grow up with pets.")
elif convo == petconvo3:
time.sleep(1)
foodbudget=input("Does it cost a lot to feed " + petlower + "?")
time.sleep(.5)

Related

Python3 printing user input with strings on a new line in a video game

Python beginner here, couple months in, still confused and still having mayjor impostor syndrome.
I was playing some Diablo2 Resurrected and while i played i was imagining code in my mind, especially when i used spells on my mage like thunderbolt which does 1-114 damage on my current level ;)
So i alt-tabbed and wrote this code:
import random
#For the random thunderbolt spell damage
thunderbolt_damage = random.randint(1,114)
#Asking for user input
mouse_clicks = int(input("How often do you want to click to use thunderbolt? "))
def thunder_spell():
mana = 120
'''Mana at my current level in Diablo2'''
while mouse_clicks > 0:
mana += mouse_clicks * -9
break
print("You have " + str(mana) + " Mana left")
print("Your thunderbolt just did " + str(thunderbolt_damage) + " damage")
thunder_spell()
Problems: I want to be able to print the number of times that i click with the mouse and display the random damage each time, on a new line. I tried several things but nothing worked. I was running into errors. I think it could be done with a for loop but i don't know how. I learned using codecademy only mostly.
import random
thunderbolt_damage = random.randint(1,114)
#Asking for user input
mouse_clicks = int(input("How often do you want to click to use thunderbolt? "))
mana = 120
def thunder_spell(mana):
for i in range(0, mouse_clicks):
mana -= 9
#For the random thunderbolt spell damage
thunderbolt_damage = random.randint(1,114)
print("Your thunderbolt just did " + str(thunderbolt_damage) + " damage")
return mana
print("You have " + str(thunder_spell(mana)) + " Mana left")
If I understand what you are asking for, I believe this will work

Python user input (lists)

Hi again i was coding my text based game and came across another problem. My code:
print("\nPath 1 - bookkeeper", "\n Path 2 - mississippi", "\n Path 3 - sleeplessness", "\n Path 4 - keenness", "\n Path 5 - suddenness")
path_way = input("Scan the word you want by "
"\ntyping here to figure out the number associated "
"\nwith each letter to add them up : ")
foreign_lang = []
occurences = []
for character in path_way:
if character not in foreign_lang:
foreign_lang.append(character)
occurences.append(1)
else:
foreign_index = foreign_lang.index(character)
occurences[foreign_index] = occurences[foreign_index] + 1
for index in range(len(foreign_lang)):
print(foreign_lang[index], "-->", occurences[index])
print("Now that you added up you numbers use that number and")
while True:
try:
act_3 = int(input("Enter the number of henchmen you want to fight : "))
if act_3==8:
print("\n You easily fight the 8 henchmen and defeat them to proceed with the path!")
break
else:
print("Sorry you have been killed as the henchmen overwhelmed you as there were too many of them")
except:
print("Sorry you have been kicked from the agency for quitting on the mission")
So I want to lead the user back to the list question instead of the while loop question if they enter the wrong answer in the while loop question. Does anyone know how I can do it?
https://trinket.io/python3/6f8defd82c
If I am comprehending your question correctly, then you can simply create a function and call it in your while loop.
def func():
print("\nPath 1 - bookkeeper", "\n Path 2 - mississippi",
"\n Path 3 - sleeplessness", "\n Path 4 - keenness",
"\n Path 5 - suddenness")
path_way = input("Scan the word you want by "
"\ntyping here to figure out the number associated "
"\nwith each letter to add them up : ")
foreign_lang = []
occurences = []
for character in path_way:
if character not in foreign_lang:
foreign_lang.append(character)
occurences.append(1)
else:
foreign_index = foreign_lang.index(character)
occurences[foreign_index] = occurences[foreign_index] + 1
for index in range(len(foreign_lang)):
print(foreign_lang[index], "-->", occurences[index])
print("Now that you added up you numbers use that number and")
while True:
func()
try:
act_3 = int(input("Enter the number of henchmen you want to fight : "))
if act_3 == 8:
print("\n You easily fight the 8 henchmen and defeat them to proceed with the path!")
break
else:
print("Sorry you have been killed as the henchmen overwhelmed you as there were too many of them")
except:
print( "Sorry you have been kicked from the agency for quitting on the mission")
This will show your list of paths/questions until user enters correct option.

TypeError: '>=' not supported between instances of 'int' and 'str' even with adding int to the front of score

score = int(0)
name = str(input("What is your name?"))
print("Hello " + name + "!")
pi = input( name + ", can you tell me the value of pi? ")
if score >= '6' :
print ("congratulations, " +name + " you passed the exam! You'll be richer than your wildest dreams!" )
else:
print: ("Listen, " +name + " you screwed up big time, with grades like this you'll be stuck working in a Best Buy for the rest of your life")
The problem is the if score statement, that's where I get the error. I need to have this "quiz" graded. I've been working on this all night, I'm not a good coder. Please help me
thank you in advance
You are comparing int (score) with string ('6'). Try:
if score > 6: # <--omit the quotes
etc...
The int in score = int(0) isn't neccessary. You can just write score = 0. For the comparison, do you really want to use the number 6 as a string? If not then just do this score >= 6 and it should work. I didn't test it but the code below should work.
score = 0
name = input("What is your name?")
print("Hello " + name + "!")
pi = input(name + ", can you tell me the value of pi?")
if score >= 6:
print("congratulations, " + name + " you passed the exam! You'll be richer than your wildest dreams!")
else:
print("Listen, " + name + " you screwed up big time, with grades like this you'll be stuck working in a Best Buy for the rest of your life")

Self teaching python. Having trouble with score incrementing after each round

TL:DR Trying to set up a scoring system, it doesn't seem to go up when you get than answer right
As the title says I'm teaching myself python, thus this is only the second code I have written (hints in why I'm learning python). I'm more than happy to take criticism on everything from syntax to how to better comment. With that being said, let's get to my issue.
This is a small guessing game. The book I'm reading taught the "for guessTaken" and subsequent code. My problem is in one small aspect of my code. The scoring system won't increment.
I set up the code in the for loop called games then try to have it display at that start of each round and go up with each correct guess. However, it will display 0 for the first few rounds then it will show 2 ( or whatever your current score is I think...). I think the problem is I'm calling the score +1 int in an if statement but I've moved the code around and can't figure it out.
I am aware that it's not beautiful! There are also a few bugs (number of games played isn't what you enter.)Right now I'm only working on the scoring system.
#this is a guess the number game.
import random
#Get's user's name
print("Hello. Welcome to Print's guessing game. Please enter your name: ")
userName = input()
#askes if they are ready
print("Are you ready to play " + userName + "?")
ready = input().lower()
if ready == 'yes' :
print("Let's get started")
else:
while ready != 'yes':
print("Let me know when you are ready")
ready = input().lower()
#Game start
#number of games
games = int(input("How many games would you like to play?"))
if games >= 1:
print("Let's get started")
for games in range (int(games), 1, -1):
while games != 1:
print("I am thinking of a number between 1 and 20 ")
secretNumber = random.randint(1, 20)
score = 0
print("Current score: " + str(score))
print("Debug: " + str(secretNumber))
for guessTaken in range (7, 1, -1):
print("you have " + str(guessTaken - 1 ) + " guesses left")
guess = int(input())
if guess < secretNumber:
print("Your guess is to low. Please guess again")
elif guess > secretNumber:
print("Your guess is too high. Maybe something a little lower?")
else:
break # This conditon is for the right guess.
if guess == secretNumber:
print("good Job " + userName + "! you guessed the number right!")
score = int(score + 1)
print("your score is " + str(score))
games = int(games - 1)
else:
print("Nope, the number I was thinking of was " + str(secretNumber))
print("you have " + str(games) + " games left")
elif games == 0:
while games == 0:
print("Would you like to exit? Yes or No ")
exit = input().lower()
if exit == 'yes':
quit()
else:
games = int(input("How many games would you like to play?"))
else:
print("wtf")
Your score variable is being initialized to zero every time your code goes through the while loop. If you want it to track the total score for all of the games, initialize it right after your print("Let's get started"). This will reset their score whenever they tell you how many games they want to play.
If you don't want their score to reset until they quit your program, you will have to initialize it at the beginning of your program. Up by your import random would work well.

Python: Loop Prompt Back Around

A lot of the errors of this Python file have been fixed but there is one last thing it's not working. I need the else statement to loop back to ask the question again if neither Yes or No is entered. You can probably see by the code what I was going for but I'm probably not even on the right track. Can someone help me with this one last thing?
#These are my variables which are just strings entered by the user.
friend = raw_input("Who is your friend? ")
my_name = raw_input("Enter Your Name: ")
game_system = raw_input("What's your favorite game system? ")
game_name = raw_input("What's your favorite game for that system? ")
game_status = raw_input("Do you have the game? (Yes or No) ")
game_store = raw_input("What is your favorite game store? ")
game_price = raw_input("What's the price of the game today? Enter a whole number. ")
#This is what is printed after all the prompts. There is no condition for this print.
print "I went outside today and my friend " + friend + " was outside waiting for me. He said \"" + my_name + ", did you get the new " + game_system + " game yet? You were getting " + game_name + " today, right?\""
#If the condition on the Yes or No question is yes, then this code runs.
if game_status == "YES":
print "\"You know I got it, man!\" I said. \"Awesome! Let's play it,\" he said. \"I heard " + game_name + " is supposed to be amazing!\" We went back inside and took turns playing until " + friend + " had to go home. Today was a fun day."
#If the condition is No, then this code runs.
elif game_status == "No":
print "\"Well let's go get it today and we can come back here and play it!\" We went down to " + game_store + " and baught " + game_name + " for $" + str(game_price) + " and we went back to my house to take turns playing until " + friend + " went home. Today was a good day. (Now try again with the No option!)"
#If the condition meets neither Yes or No, then this code runs, sending the user back to the same question again. This repeats until a condition is met.
else:
raw_input("That answer didn't work. Try again? Do you have the game? (Yes or No) ")
This:
if game_status: "YES"
isn't how you make an if statement. You're treating it like the syntax is
if some_variable: some_value
and if the variable has that value, the if statement triggers. In fact, the syntax is
if some_expression:
and if the expression evaluates to something considered true, the if statement triggers. When you want the if statement to trigger on game_status equalling "YES", the expression should be game_status == "YES", so the if line should go
if game_status == "YES":
Similarly, the elif line should go
elif game_status == "NO":
I would encourage to you always break to a new line after a conditional...
if game_status == "YES":
print "\"You know I got it, man!\" I said. \"Awesome! Let's play it,\" he said. \"I heard " + game_name + " is supposed to be amazing!\" We went back inside and took turns playing until " + friend + " had to go home. Today was a fun day."
anything that is indented after the "if game_status:" will get run. And it reads better.
edit:: if you use single quotes for all strings then you don't need to escape the double quotes...
print '"You know I got it, man!" I said. "Awesome! Let\'s play it," he said. "I heard ' + game_name + ' is supposed to be amazing!" We went back inside and took turns playing until ' + friend + ' had to go home. Today was a fun day.'
it's a matter of preference...but may look less cluttered.

Categories