i’m a beginner and i’m just starting to learn how to python code. I’m having trouble with this one. Whenever I type in the correct result, it shows that it is incorrect still. I'm wondering what i'm missing or what I did wrong.
array1 = ([5, 10, 15, 20, 25])
print("Question 2: What is the reverse of the following array?", array1)
userAns = input("Enter your answer: ")
array1.reverse()
arrayAns = array1
if userAns == arrayAns:
print("You are correct")
else:
print("You are incorrect")
CodePudding user response:
When you use input()
the assigned variable will default to type string
therefore, always returning false when compared to an array.
However, if you plan to return a list in a string format, you should try using ast.literal_eval() to evaluate the string you passed as answer to the input function.
Consider:
import ast
userAns = ast.literal_eval(input("Enter your answer: "))
And after sending:
[25,20,15,10,5]
You will get as result:
You are correct
Because the string you passed as answer to the question ('[25,20,15,10,5]') will get evaluated and recognized as a list, and then, when comparing it to the other variable, will evaluate to True.
CodePudding user response:
input
only returns a str
value. You'd have to either turn the "reversed" array into a str
or turn the input into a numpy array. The first option seems easier and could be str(array1)
.
CodePudding user response:
As already mentioned you are trying to compare the user input which is a string to an array so it's going to come out false when comparing.
My solution is:
temp = userAns.split(",")
userAns = [int(item) for item in temp]
First split the string into a list. This will create an array of strings. Next recreate array by changing each item type from string to int. You end up with an array of integers which then can be compared.
CodePudding user response:
As the other responders have indicated, input
is returning a str
. If you'd like, you can ask the user to enter values in a comma delimited format and then use the split()
function to break the string into an array separated by the comma as follows:
array1 = ([5, 10, 15, 20, 25])
print("Question 2: What is the reverse of the following array?", array1)
userAns = input("Enter your answer (comma delimited, please): ")
array1.reverse()
arrayAns = list(map( str, array1 ) )
if userAns.split(',') == arrayAns:
print("You are correct")
else:
print("You are incorrect")
Here's example output:
Question 2: What is the reverse of the following array? [5, 10, 15, 20, 25]
Enter your answer (comma delimited, please): 25,20,15,10,5
You are correct
And another run - let's make sure it works when they enter the wrong values:
Question 2: What is the reverse of the following array? [5, 10, 15, 20, 25]
Enter your answer (comma delimited, please): 25,20,15,10,6
You are incorrect
Since the user is entering strings, we need to compare strings to strings. And so, I converted every integer in the array1 to a string using the map( str, array1)
call below.
You can also see that I split up the userAns
into an array using split(',')
.
An opposite approach is also possible. We can build a string out of array1 and just compare the strings. In this case, I can use the join()
function to build a string from an array:
arrayString = ','.join( arrayAns )
And so the code could look like this:
array1 = ([5, 10, 15, 20, 25])
print("Question 2: What is the reverse of the following array?", array1)
userAns = input("Enter your answer (comma delimited, please): ")
array1.reverse()
arrayAns = list(map( str, array1 ) )
arrayString = ','.join(arrayAns)
if userAns == arrayString:
print("You are correct")
else:
print("You are incorrect")