Reputation: 33
my program askTheUser()
works the way I want it too but I'm somehow supposed to change it to include either a "for" or "while" loop. Any suggestions? The ending result is supposed to be a list of zeros and ones. For example, [1,1,1,1,1] would mean they would like to re-roll every dice.
import random
def askTheUser():
userList = []
choice = input("Would you like to re-roll dice 1? If so, enter 'Yes'. If not, enter 'No'.")
if choice == str("Yes"):
userList.append(1)
else:
userList.append(0)
choice2 = input("Would you like to re-roll dice 2? If so, enter 'Yes'. If not, enter 'No'.")
if choice2 == str("Yes"):
userList.append(1)
else:
userList.append(0)
choice3 = input("Would you like to re-roll dice 3? If so, enter 'Yes'. If not, enter 'No'.")
if choice3 == str("Yes"):
userList.append(1)
else:
userList.append(0)
choice4 = input("Would you like to re-roll dice 4? If so, enter 'Yes'. If not, enter 'No'.")
if choice4 == str("Yes"):
userList.append(1)
else:
userList.append(0)
choice5 = input("Would you like to re-roll dice 5? If so, enter 'Yes'. If not, enter 'No'.")
if choice5 == str("Yes"):
userList.append(1)
else:
userList.append(0)
return userList
Upvotes: 1
Views: 96
Reputation: 387697
As soon as you realize that you are writing the same code over and over again, you should think about how to automate that. In your case, the only thing that actually changes in your 5 parts is the dice number in the text, and the variable the result is being assigned to.
So the first step would be to make the input text accept a variable dice number. We can use string formatting for that:
"Would you like to re-roll dice {}? If so, enter 'Yes'. If not, enter 'No'.".format(i)
Now the value of i
will be inserted in the place of {}
in the string.
Next, we should think about those choice
variables. If you think about it, we don’t actually need separate ones for what they do. After all, they only capture the user input, and immediately after we evaluate the value and then forget about them. So we can just name them all choice
overwriting the input each time.
So for dice number i
, we have this as the relevant code (also note that str("something")
is equivalent to just "something"
):
choice = input("Would you like to re-roll dice {}? If so, enter 'Yes'. If not, enter 'No'.".format(i))
if choice == "Yes":
userList.append(1)
else:
userList.append(0)
The only thing that changes now is the value of i
, which should go from 1
to 5
. So we use a loop for that, and that’s all:
def askTheUser():
userList = []
for i in range(1, 6):
choice = input("Would you like to re-roll dice {}? If so, enter 'Yes'. If not, enter 'No'.".format(i))
if choice == "Yes":
userList.append(1)
else:
userList.append(0)
return userList
Btw. as you are collecting “decisions” whether or not to reroll individual dice, it would actually make more sense to store boolean values True
and False
instead of 1
and 0
. So you would append True
or False
instead:
if choice == "Yes":
userList.append(True)
else:
userList.append(False)
And in that case, you can then just append the result from the comparison directly instead:
userList.append(choice == "Yes")
And then, when you later check the values, instead of having to check if decision == 1
, you can just do if decision
.
Upvotes: 5
Reputation: 179
def askTheUser():
userList = []
for i in range(5):
choice = raw_input("Would you like to re-roll dice "+str(i)+"? If so, enter 'Yes'. If not, enter 'No'.")
if choice == str("Yes"):
userList.append(1)
else:
userList.append(0)
return userList
Please check this!!
Upvotes: 0
Reputation: 34017
def askTheUser():
userList=[]
for i in range(5):
choice = input("Would you like to re-roll dice %d? If so, enter 'Yes'. If not, enter 'No'."%(i+1))
userList.append(1 if choice == 'Yes' else 0)
return userList
Upvotes: 0
Reputation: 6657
This would work.
def askTheUser():
userList = []
for i in range(1,6):
question = "Would you like to re-roll dice " + str(i) + "? If so, enter 'Yes'. If not, enter 'No'."
choice = input(question)
if choice == str("Yes"):
userList.append(1)
else:
userList.append(0)
return userList
Upvotes: 0