How can I get choice function to return a different result under the while loop

Question:

I have this code that works fine, but there’s a minor issue. The first choice function works well and returns a random state in the question. And if the answer is yes, it works fine and prints what I want. But when the answer is no, I’d like the question to come with a different state name. But the second choice function, under the while loop, returns the same state as the first question. I thought another usage of choice would return another random state name but apparently, it’s not so.

Here’s the code:

from random import choice

states = {
        'AK': 'Alaska',
        'AL': 'Alabama',
        'AR': 'Arkansas',
        'AS': 'American Samoa',
        'AZ': 'Arizona',
        'CA': 'California',
        'CO': 'Colorado',
        'CT': 'Connecticut',
        'DC': 'District of Columbia',
        'DE': 'Delaware',
        'FL': 'Florida',
        'GA': 'Georgia',
        'GU': 'Guam',
        'HI': 'Hawaii',
        'IA': 'Iowa',
        'ID': 'Idaho',
        'IL': 'Illinois',
        'IN': 'Indiana',
        'KS': 'Kansas',
        'KY': 'Kentucky',
        'LA': 'Louisiana',
        'MD': 'Maryland',
        'MA': 'Massachusetts',
        'ME': 'Maine',
        'MI': 'Michigan',
        'MN': 'Minnesota',
        'MO': 'Missouri',
        'MP': 'Northern Mariana Islands',
        'MS': 'Mississippi',
        'MT': 'Montana',
        'NA': 'National',
        'NC': 'North Carolina',
        'ND': 'North Dakota',
        'NE': 'Nebraska',
        'NH': 'New Hampshire',
        'NJ': 'New Jersey',
        'NM': 'New Mexico',
        'NV': 'Nevada',
        'NY': 'New York',
        'OH': 'Ohio',
        'OK': 'Oklahoma',
        'OR': 'Oregon',
        'PA': 'Pennsylvania',
        'PR': 'Puerto Rico',
        'RI': 'Rhode Island',
        'SC': 'South Carolina',
        'SD': 'South Dakota',
        'TN': 'Tennessee',
        'TX': 'Texas',
        'UT': 'Utah',
        'VA': 'Virginia',
        'VI': 'Virgin Islands',
        'VT': 'Vermont',
        'WA': 'Washington',
        'WI': 'Wisconsin',
        'WV': 'West Virginia',
        'WY': 'Wyoming'
}

state_list = list(states.values())

choice = choice(state_list)

question = "Would you like to move to {}?: ".format(choice)

answer = input(question).strip().lower()

while answer!= "yes":

    question = "Okay. How about {}?: ".format(choice)
    answer = input(question).strip().lower()

print("Sounds good! Let's go there!")   
Asked By: Alper

||

Answers:

Don’t forget to change the choice variable in every iteration, otehrwise it will chose the same state each loop. Try this:

while answer!= "yes":
    choice = choice(state_list)
    question = "Okay. How about {}?: ".format(choice)
    answer = input(question).strip().lower()

You should rename your choice variable with something that is not a function name and it should be working

Answered By: Sz. Zsolt

Explanation

Your problem is that you find random from state_list

choice = choice(state_list)

and you treat is as function -> you think whenever you use it, it will generate random choice. No. Choice is a variable in which you saved random from state_list. In your loop, you are basicly calling choice which is nothing more than saved variable.

You can however create function generate_choice() that will do what you want

import random
def generate_choice():
    return random.choice(state_list)

so now you can always call

new_choice = generate_choice()

to get new random element from state_list

Also you did great mistake by doing

choice = choice(state_list)

because you destroyed your imported function choice and you put instead of this function a result of this function, so now you cant call it again.

Solution

...
#
#choice = choice(state_list) <-- here you destroyed your function
# instead import module like this:
import random

choice = random.choice(state_list) <--- now you are save
question = "Would you like to move to {}?: ".format(choice) 

answer = input(question).strip().lower()

while answer!= "yes":

    question = "Okay. How about {}?: ".format(random.choice(state_list)) # generate choice everytime new
    answer = input(question).strip().lower()

print("Sounds good! Let's go there!")   
Answered By: Martin
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.