简体   繁体   中英

How to save a user's input as a variable?

I am learning Python and trying to make a program that asks:

  1. Where are you from? if user responds "Buenos Aires" the program asks
  2. Should we kill em all?

That part is done. What I want to do next is make it so that if the user responds to 2 with "yes" the program responds with "Damn straight soldier." Best I can think of is that I should have the user's input from 2 saved as a variable and write something that says if that variable = yes then print("damn straight soldier"). But how do I designate the user input in response to 2 as a variable?

Current code:

user_location = input("where are you from?:")
if user_location == "Buenos Aires":
print(input("should we kill em all?:"))

Round 2:

user_location = input("where are you from?:")
Buenos_Aires = input("should we kill em all?:")
if user_location == "Buenos Aires":
    print(Buenos_Aires)
    if Buenos_Aires == "yes":
        print("damn straight soldier")

Round 3:

user_location = input("where are you from?:")
if user_location == "Buenos Aires":
    print(Buenos_Aires)

Buenos_Aires = input("should we kill em all?:")
if Buenos_Aires == "yes":
        print("damn straight soldier")

Round 4: (still getting "yes" as the end here)

user_location = input("where are you from?:")

if user_location == "Buenos Aires":
    kill_em_all = input("should we kill em all?:")

    if kill_em_all == "yes":
        print("Damn Straight Soldier")

Round 5 - Copy Paste Code我复制粘贴的代码 RMills 说适用于他的系统,但仍然给我“是”作为结尾。

What I'm proposing is a slight change to your round 3 code.

user_location = input("where are you from?:")

if user_location == "Buenos Aires":    
    kill_em_all = input("should we kill em all?:")

    if kill_em_all == "yes":
        print("damn straight soldier")

This looks to follow your requirements above.

Note that if they don't say they are from Buenos Aires, the program will end.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM