1

I'm writing a code that allows a user to enter a city they have been to. After the user inputs it, I want my code to return a randomly generated remark about the city from my list. However, whenever I run the code, it concatenates the user input with a random letter, which is not my intention of the code.

import random

message = "Type your city here: "

#Comments to concatenate with user input
comments = [f"what a lovely {}", f"I always wanted to visit {}", "I hope you enjoyed your trip to {}"]

#While loop for user input
while True:
   message = input(message)

   for elem in comments:
      message += random.choice(elem)

   if message == "quit":
      break
2
  • 2
    You can't use f-strings like that. You have to use .format on the concatenated string Commented Apr 8, 2021 at 19:40
  • 1
    Surely you want to check for quit before constructing the message. You do not want the f prefix on those strings. You destroy your prompt message the first time through the loop. You don't need a for loop in there, just print(random.choice(comments).format(message)). Commented Apr 8, 2021 at 19:40

4 Answers 4

1

I assume this is what your looking for?

import random
#Comments to concatenate with user input 
comments = ["what a lovely ", "I always wanted to visit ", "I hope you enjoyed your trip to "]

#While loop for user input
message = None
while message != "quit":
   message = input("Type your city here: ")
   print(random.choice(comments)+message)
Sign up to request clarification or add additional context in comments.

2 Comments

It will crash because the message is not defined in the while.
Thank you very much! The code runs exactly as intended. Edit: don't worry about the 'message' variable, I defined it. Much appretiated.
0

I recommend coding a function that takes the city as input then at the end returns the list. Like this

def random_quote(city):
    comments = [f"what a lovely ", f"I always wanted to visit ", "I hope you 
     enjoyed your trip to "]
    comment = random.choice(comments)
    return comment + city

Comments

0

random.choice() accepts a list (take a look at the docs), Don't iterate over your comments variable, pass it to random.choice() and don't forget to replace {} with the city:

city = input('Please enter a city')

comment = random.choice(comments)

comment.replace('{}', city)

print(comment)

Comments

0

You do not need a for loop inside your while. You should always avoid while True as it is an opening for bugs. Having a break inside a loop usually marks bad programming.

You should probably read a bit about what f-string is before using it, you also don't seem to know what random.choice does since you put it into the for which gave it the messages, which it randomly took a character out of.

import random


def main():
    prompt = "Type your city here: "

    # Comments to concatenate with user input
    comments = ["what a lovely ", "I always wanted to visit ", "I hope you enjoyed your trip to "]

    usr_input = input(prompt)
    # While loop for user input
    while usr_input != 'quit':
        message = random.choice(comments) + usr_input
        usr_input = input(prompt)


if __name__ == '__main__':
    main()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.