Python Random Number Game defaults to wrong answer
I'm trying to make a Simple Python program that asks the user to guess a number between 1 and ten and takes advantage of the randint function in python.
At first everything seemed great as it would for sure mark a wrong answer as wrong, but when I kept trying it to see the permutations it marked a right answer as wrong.
Here is the code:
import random
cont = True
while cont:
var1 = random.randint(1, 10)
#var1 = 6
print("Pick a number between One and Ten")
varans = input()
#varans = 6
if varans == var1:
print("The right answer was...")
print(var1)
print("Your Answer was...")
print(varans)
print("You chose wisely. You win!")
elif varans != var1:
print("The right answer was...")
print(var1)
print("Your Answer was...")
print(varans)
print("Sorry, You lose!")
print("Do you want to continue?")
choice = input()
choice = choice.lower()
if choice != "y":
cont = False
the commented parts were from when I was testing it to see if it was even evaluating anything. I hardcoded a Correct answer to see if that part would work. The odd thing is it did work but for some reason, I can't get it to work with the original code. I'm probably missing something simple. Any ideas?
python
add a comment |
I'm trying to make a Simple Python program that asks the user to guess a number between 1 and ten and takes advantage of the randint function in python.
At first everything seemed great as it would for sure mark a wrong answer as wrong, but when I kept trying it to see the permutations it marked a right answer as wrong.
Here is the code:
import random
cont = True
while cont:
var1 = random.randint(1, 10)
#var1 = 6
print("Pick a number between One and Ten")
varans = input()
#varans = 6
if varans == var1:
print("The right answer was...")
print(var1)
print("Your Answer was...")
print(varans)
print("You chose wisely. You win!")
elif varans != var1:
print("The right answer was...")
print(var1)
print("Your Answer was...")
print(varans)
print("Sorry, You lose!")
print("Do you want to continue?")
choice = input()
choice = choice.lower()
if choice != "y":
cont = False
the commented parts were from when I was testing it to see if it was even evaluating anything. I hardcoded a Correct answer to see if that part would work. The odd thing is it did work but for some reason, I can't get it to work with the original code. I'm probably missing something simple. Any ideas?
python
2
Welcome to SO! You're comparing a string and an integer"6" == 6
?. They'll never be equal. Use a castint(input())
.
– ggorlen
Nov 25 '18 at 4:08
Thanks a bunch! That did it! I knew it was something simple. I forgot about casting. I'm still very new to python so this helped a lot.
– Andrew Davidson
Nov 25 '18 at 4:12
1
It's likely happened to everyone that's ever learned the language. Welcome aboard the Python Train.
– ggorlen
Nov 25 '18 at 4:16
add a comment |
I'm trying to make a Simple Python program that asks the user to guess a number between 1 and ten and takes advantage of the randint function in python.
At first everything seemed great as it would for sure mark a wrong answer as wrong, but when I kept trying it to see the permutations it marked a right answer as wrong.
Here is the code:
import random
cont = True
while cont:
var1 = random.randint(1, 10)
#var1 = 6
print("Pick a number between One and Ten")
varans = input()
#varans = 6
if varans == var1:
print("The right answer was...")
print(var1)
print("Your Answer was...")
print(varans)
print("You chose wisely. You win!")
elif varans != var1:
print("The right answer was...")
print(var1)
print("Your Answer was...")
print(varans)
print("Sorry, You lose!")
print("Do you want to continue?")
choice = input()
choice = choice.lower()
if choice != "y":
cont = False
the commented parts were from when I was testing it to see if it was even evaluating anything. I hardcoded a Correct answer to see if that part would work. The odd thing is it did work but for some reason, I can't get it to work with the original code. I'm probably missing something simple. Any ideas?
python
I'm trying to make a Simple Python program that asks the user to guess a number between 1 and ten and takes advantage of the randint function in python.
At first everything seemed great as it would for sure mark a wrong answer as wrong, but when I kept trying it to see the permutations it marked a right answer as wrong.
Here is the code:
import random
cont = True
while cont:
var1 = random.randint(1, 10)
#var1 = 6
print("Pick a number between One and Ten")
varans = input()
#varans = 6
if varans == var1:
print("The right answer was...")
print(var1)
print("Your Answer was...")
print(varans)
print("You chose wisely. You win!")
elif varans != var1:
print("The right answer was...")
print(var1)
print("Your Answer was...")
print(varans)
print("Sorry, You lose!")
print("Do you want to continue?")
choice = input()
choice = choice.lower()
if choice != "y":
cont = False
the commented parts were from when I was testing it to see if it was even evaluating anything. I hardcoded a Correct answer to see if that part would work. The odd thing is it did work but for some reason, I can't get it to work with the original code. I'm probably missing something simple. Any ideas?
python
python
asked Nov 25 '18 at 4:05
Andrew DavidsonAndrew Davidson
31
31
2
Welcome to SO! You're comparing a string and an integer"6" == 6
?. They'll never be equal. Use a castint(input())
.
– ggorlen
Nov 25 '18 at 4:08
Thanks a bunch! That did it! I knew it was something simple. I forgot about casting. I'm still very new to python so this helped a lot.
– Andrew Davidson
Nov 25 '18 at 4:12
1
It's likely happened to everyone that's ever learned the language. Welcome aboard the Python Train.
– ggorlen
Nov 25 '18 at 4:16
add a comment |
2
Welcome to SO! You're comparing a string and an integer"6" == 6
?. They'll never be equal. Use a castint(input())
.
– ggorlen
Nov 25 '18 at 4:08
Thanks a bunch! That did it! I knew it was something simple. I forgot about casting. I'm still very new to python so this helped a lot.
– Andrew Davidson
Nov 25 '18 at 4:12
1
It's likely happened to everyone that's ever learned the language. Welcome aboard the Python Train.
– ggorlen
Nov 25 '18 at 4:16
2
2
Welcome to SO! You're comparing a string and an integer
"6" == 6
?. They'll never be equal. Use a cast int(input())
.– ggorlen
Nov 25 '18 at 4:08
Welcome to SO! You're comparing a string and an integer
"6" == 6
?. They'll never be equal. Use a cast int(input())
.– ggorlen
Nov 25 '18 at 4:08
Thanks a bunch! That did it! I knew it was something simple. I forgot about casting. I'm still very new to python so this helped a lot.
– Andrew Davidson
Nov 25 '18 at 4:12
Thanks a bunch! That did it! I knew it was something simple. I forgot about casting. I'm still very new to python so this helped a lot.
– Andrew Davidson
Nov 25 '18 at 4:12
1
1
It's likely happened to everyone that's ever learned the language. Welcome aboard the Python Train.
– ggorlen
Nov 25 '18 at 4:16
It's likely happened to everyone that's ever learned the language. Welcome aboard the Python Train.
– ggorlen
Nov 25 '18 at 4:16
add a comment |
2 Answers
2
active
oldest
votes
input
will return a string. Therefore, it will never be equal to any int
.
You can either convert your random integer to a string
var1 = str(random.randint(1, 10))
Or covert the user input from a string to an integer
varans = int(input())
And all should be well in the world.
Glad it helped, and welcome to SO. One important thing you'll learn quickly about SO is that, as a matter of keeping things quality here, contributors can get a bit picky about the questions that are asked. I encourage you to check out the how to ask article to see what kind of qualities we look for in questions. Whether learning Python or how to use SO, we all start somewhere! Good luck!
– sytech
Nov 25 '18 at 6:07
add a comment |
I believe that the issue with your code is that you are comparing an int to a string. To fix this you can do either varans = int(input())
or var1 = str(random.randint(1, 10))
. Cheers.
Thanks! That fixed it. For some reason casting completely slipped my mind and I thought it would pull the input in as an int automatically. My bad. Thanks for the help!
– Andrew Davidson
Nov 25 '18 at 4:13
@AndrewDavidson No problem, I am glad to help. Feel free to mark the one either of the answers as the solution. Therefore, if someone else with a similar problem stumbles upon this thread in the future, they can easily determine the solution that worked for you.
– The Pineapple
Nov 25 '18 at 4:17
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464559%2fpython-random-number-game-defaults-to-wrong-answer%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
input
will return a string. Therefore, it will never be equal to any int
.
You can either convert your random integer to a string
var1 = str(random.randint(1, 10))
Or covert the user input from a string to an integer
varans = int(input())
And all should be well in the world.
Glad it helped, and welcome to SO. One important thing you'll learn quickly about SO is that, as a matter of keeping things quality here, contributors can get a bit picky about the questions that are asked. I encourage you to check out the how to ask article to see what kind of qualities we look for in questions. Whether learning Python or how to use SO, we all start somewhere! Good luck!
– sytech
Nov 25 '18 at 6:07
add a comment |
input
will return a string. Therefore, it will never be equal to any int
.
You can either convert your random integer to a string
var1 = str(random.randint(1, 10))
Or covert the user input from a string to an integer
varans = int(input())
And all should be well in the world.
Glad it helped, and welcome to SO. One important thing you'll learn quickly about SO is that, as a matter of keeping things quality here, contributors can get a bit picky about the questions that are asked. I encourage you to check out the how to ask article to see what kind of qualities we look for in questions. Whether learning Python or how to use SO, we all start somewhere! Good luck!
– sytech
Nov 25 '18 at 6:07
add a comment |
input
will return a string. Therefore, it will never be equal to any int
.
You can either convert your random integer to a string
var1 = str(random.randint(1, 10))
Or covert the user input from a string to an integer
varans = int(input())
And all should be well in the world.
input
will return a string. Therefore, it will never be equal to any int
.
You can either convert your random integer to a string
var1 = str(random.randint(1, 10))
Or covert the user input from a string to an integer
varans = int(input())
And all should be well in the world.
answered Nov 25 '18 at 4:09
sytechsytech
4,73711131
4,73711131
Glad it helped, and welcome to SO. One important thing you'll learn quickly about SO is that, as a matter of keeping things quality here, contributors can get a bit picky about the questions that are asked. I encourage you to check out the how to ask article to see what kind of qualities we look for in questions. Whether learning Python or how to use SO, we all start somewhere! Good luck!
– sytech
Nov 25 '18 at 6:07
add a comment |
Glad it helped, and welcome to SO. One important thing you'll learn quickly about SO is that, as a matter of keeping things quality here, contributors can get a bit picky about the questions that are asked. I encourage you to check out the how to ask article to see what kind of qualities we look for in questions. Whether learning Python or how to use SO, we all start somewhere! Good luck!
– sytech
Nov 25 '18 at 6:07
Glad it helped, and welcome to SO. One important thing you'll learn quickly about SO is that, as a matter of keeping things quality here, contributors can get a bit picky about the questions that are asked. I encourage you to check out the how to ask article to see what kind of qualities we look for in questions. Whether learning Python or how to use SO, we all start somewhere! Good luck!
– sytech
Nov 25 '18 at 6:07
Glad it helped, and welcome to SO. One important thing you'll learn quickly about SO is that, as a matter of keeping things quality here, contributors can get a bit picky about the questions that are asked. I encourage you to check out the how to ask article to see what kind of qualities we look for in questions. Whether learning Python or how to use SO, we all start somewhere! Good luck!
– sytech
Nov 25 '18 at 6:07
add a comment |
I believe that the issue with your code is that you are comparing an int to a string. To fix this you can do either varans = int(input())
or var1 = str(random.randint(1, 10))
. Cheers.
Thanks! That fixed it. For some reason casting completely slipped my mind and I thought it would pull the input in as an int automatically. My bad. Thanks for the help!
– Andrew Davidson
Nov 25 '18 at 4:13
@AndrewDavidson No problem, I am glad to help. Feel free to mark the one either of the answers as the solution. Therefore, if someone else with a similar problem stumbles upon this thread in the future, they can easily determine the solution that worked for you.
– The Pineapple
Nov 25 '18 at 4:17
add a comment |
I believe that the issue with your code is that you are comparing an int to a string. To fix this you can do either varans = int(input())
or var1 = str(random.randint(1, 10))
. Cheers.
Thanks! That fixed it. For some reason casting completely slipped my mind and I thought it would pull the input in as an int automatically. My bad. Thanks for the help!
– Andrew Davidson
Nov 25 '18 at 4:13
@AndrewDavidson No problem, I am glad to help. Feel free to mark the one either of the answers as the solution. Therefore, if someone else with a similar problem stumbles upon this thread in the future, they can easily determine the solution that worked for you.
– The Pineapple
Nov 25 '18 at 4:17
add a comment |
I believe that the issue with your code is that you are comparing an int to a string. To fix this you can do either varans = int(input())
or var1 = str(random.randint(1, 10))
. Cheers.
I believe that the issue with your code is that you are comparing an int to a string. To fix this you can do either varans = int(input())
or var1 = str(random.randint(1, 10))
. Cheers.
answered Nov 25 '18 at 4:09
The PineappleThe Pineapple
408212
408212
Thanks! That fixed it. For some reason casting completely slipped my mind and I thought it would pull the input in as an int automatically. My bad. Thanks for the help!
– Andrew Davidson
Nov 25 '18 at 4:13
@AndrewDavidson No problem, I am glad to help. Feel free to mark the one either of the answers as the solution. Therefore, if someone else with a similar problem stumbles upon this thread in the future, they can easily determine the solution that worked for you.
– The Pineapple
Nov 25 '18 at 4:17
add a comment |
Thanks! That fixed it. For some reason casting completely slipped my mind and I thought it would pull the input in as an int automatically. My bad. Thanks for the help!
– Andrew Davidson
Nov 25 '18 at 4:13
@AndrewDavidson No problem, I am glad to help. Feel free to mark the one either of the answers as the solution. Therefore, if someone else with a similar problem stumbles upon this thread in the future, they can easily determine the solution that worked for you.
– The Pineapple
Nov 25 '18 at 4:17
Thanks! That fixed it. For some reason casting completely slipped my mind and I thought it would pull the input in as an int automatically. My bad. Thanks for the help!
– Andrew Davidson
Nov 25 '18 at 4:13
Thanks! That fixed it. For some reason casting completely slipped my mind and I thought it would pull the input in as an int automatically. My bad. Thanks for the help!
– Andrew Davidson
Nov 25 '18 at 4:13
@AndrewDavidson No problem, I am glad to help. Feel free to mark the one either of the answers as the solution. Therefore, if someone else with a similar problem stumbles upon this thread in the future, they can easily determine the solution that worked for you.
– The Pineapple
Nov 25 '18 at 4:17
@AndrewDavidson No problem, I am glad to help. Feel free to mark the one either of the answers as the solution. Therefore, if someone else with a similar problem stumbles upon this thread in the future, they can easily determine the solution that worked for you.
– The Pineapple
Nov 25 '18 at 4:17
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464559%2fpython-random-number-game-defaults-to-wrong-answer%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Welcome to SO! You're comparing a string and an integer
"6" == 6
?. They'll never be equal. Use a castint(input())
.– ggorlen
Nov 25 '18 at 4:08
Thanks a bunch! That did it! I knew it was something simple. I forgot about casting. I'm still very new to python so this helped a lot.
– Andrew Davidson
Nov 25 '18 at 4:12
1
It's likely happened to everyone that's ever learned the language. Welcome aboard the Python Train.
– ggorlen
Nov 25 '18 at 4:16