Can't make this random number generator to work properly
I'm trying to make a random number generator and return the random generated number, but this code returns all the numbers before the random number. How can I return only the last string printed?
import random
from_num = int(input('Generate a random number:nFrom:'))
to_num = int(input('To:'))
for num in range(random.randrange(from_num,to_num+1)):
if True:
print(f'Random number: {num}')
else:
print('You did not entered valid min/max numbers')
Output for from_num = 0
and to_num = 20
by exemple, instead of '11' can return any number between these two given.
Random number: 0
Random number: 1
Random number: 2
Random number: 3
Random number: 4
Random number: 5
Random number: 6
Random number: 7
Random number: 8
Random number: 9
Random number: 10
Random number: 11
python loops random
add a comment |
I'm trying to make a random number generator and return the random generated number, but this code returns all the numbers before the random number. How can I return only the last string printed?
import random
from_num = int(input('Generate a random number:nFrom:'))
to_num = int(input('To:'))
for num in range(random.randrange(from_num,to_num+1)):
if True:
print(f'Random number: {num}')
else:
print('You did not entered valid min/max numbers')
Output for from_num = 0
and to_num = 20
by exemple, instead of '11' can return any number between these two given.
Random number: 0
Random number: 1
Random number: 2
Random number: 3
Random number: 4
Random number: 5
Random number: 6
Random number: 7
Random number: 8
Random number: 9
Random number: 10
Random number: 11
python loops random
3
If you only want a single value, why on earth do you have a for loop?
– jonrsharpe
Dec 29 '18 at 18:58
1
Or on any planet for that matter.
– Paritosh Singh
Dec 29 '18 at 19:01
1
Also, yourelse
clause will never get executed withTrue
as your if statement condition.
– busybear
Dec 29 '18 at 19:02
add a comment |
I'm trying to make a random number generator and return the random generated number, but this code returns all the numbers before the random number. How can I return only the last string printed?
import random
from_num = int(input('Generate a random number:nFrom:'))
to_num = int(input('To:'))
for num in range(random.randrange(from_num,to_num+1)):
if True:
print(f'Random number: {num}')
else:
print('You did not entered valid min/max numbers')
Output for from_num = 0
and to_num = 20
by exemple, instead of '11' can return any number between these two given.
Random number: 0
Random number: 1
Random number: 2
Random number: 3
Random number: 4
Random number: 5
Random number: 6
Random number: 7
Random number: 8
Random number: 9
Random number: 10
Random number: 11
python loops random
I'm trying to make a random number generator and return the random generated number, but this code returns all the numbers before the random number. How can I return only the last string printed?
import random
from_num = int(input('Generate a random number:nFrom:'))
to_num = int(input('To:'))
for num in range(random.randrange(from_num,to_num+1)):
if True:
print(f'Random number: {num}')
else:
print('You did not entered valid min/max numbers')
Output for from_num = 0
and to_num = 20
by exemple, instead of '11' can return any number between these two given.
Random number: 0
Random number: 1
Random number: 2
Random number: 3
Random number: 4
Random number: 5
Random number: 6
Random number: 7
Random number: 8
Random number: 9
Random number: 10
Random number: 11
python loops random
python loops random
edited Dec 29 '18 at 19:24
Isaak Schimdt
asked Dec 29 '18 at 18:56
Isaak SchimdtIsaak Schimdt
82
82
3
If you only want a single value, why on earth do you have a for loop?
– jonrsharpe
Dec 29 '18 at 18:58
1
Or on any planet for that matter.
– Paritosh Singh
Dec 29 '18 at 19:01
1
Also, yourelse
clause will never get executed withTrue
as your if statement condition.
– busybear
Dec 29 '18 at 19:02
add a comment |
3
If you only want a single value, why on earth do you have a for loop?
– jonrsharpe
Dec 29 '18 at 18:58
1
Or on any planet for that matter.
– Paritosh Singh
Dec 29 '18 at 19:01
1
Also, yourelse
clause will never get executed withTrue
as your if statement condition.
– busybear
Dec 29 '18 at 19:02
3
3
If you only want a single value, why on earth do you have a for loop?
– jonrsharpe
Dec 29 '18 at 18:58
If you only want a single value, why on earth do you have a for loop?
– jonrsharpe
Dec 29 '18 at 18:58
1
1
Or on any planet for that matter.
– Paritosh Singh
Dec 29 '18 at 19:01
Or on any planet for that matter.
– Paritosh Singh
Dec 29 '18 at 19:01
1
1
Also, your
else
clause will never get executed with True
as your if statement condition.– busybear
Dec 29 '18 at 19:02
Also, your
else
clause will never get executed with True
as your if statement condition.– busybear
Dec 29 '18 at 19:02
add a comment |
3 Answers
3
active
oldest
votes
Following to the comments above, just print the random value, without iterating on anything:
import random
from_num = int(input('Generate a random number:nFrom:'))
to_num = int(input('To:'))
if from_num > to_num:
print('You did not entered valid min/max numbers')
return
random_num = random.randrange(from_num,to_num+1):
print(f'Random number: {random_num}')
This is not a complete solution as it drops the range check in the original example.
– Jason Baumgartner
Dec 29 '18 at 19:05
1
@JasonBaumgartner fixed, thanks
– ItayB
Dec 29 '18 at 19:09
add a comment |
Replace this :
for num in range(random.randrange(from_num,to_num+1)):
if True:
print(f'Random number: {num}')
else:
print('You did not entered valid min/max numbers')
with :
ran_num = random.randint(from_num,to_num)
print("Random number is " + str(ran_num))
add a comment |
Why do you have a loop?
import random
from_num = int(input('Generate a random number:nFrom:'))
to_num = int(input('To:'))
if to_num > from_num:
ran_number = random.randrange(from_num,to_num+1)
print(f'Random number: {ran_number}')
else:
print('You did not entered valid min/max numbers')
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%2f53972483%2fcant-make-this-random-number-generator-to-work-properly%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Following to the comments above, just print the random value, without iterating on anything:
import random
from_num = int(input('Generate a random number:nFrom:'))
to_num = int(input('To:'))
if from_num > to_num:
print('You did not entered valid min/max numbers')
return
random_num = random.randrange(from_num,to_num+1):
print(f'Random number: {random_num}')
This is not a complete solution as it drops the range check in the original example.
– Jason Baumgartner
Dec 29 '18 at 19:05
1
@JasonBaumgartner fixed, thanks
– ItayB
Dec 29 '18 at 19:09
add a comment |
Following to the comments above, just print the random value, without iterating on anything:
import random
from_num = int(input('Generate a random number:nFrom:'))
to_num = int(input('To:'))
if from_num > to_num:
print('You did not entered valid min/max numbers')
return
random_num = random.randrange(from_num,to_num+1):
print(f'Random number: {random_num}')
This is not a complete solution as it drops the range check in the original example.
– Jason Baumgartner
Dec 29 '18 at 19:05
1
@JasonBaumgartner fixed, thanks
– ItayB
Dec 29 '18 at 19:09
add a comment |
Following to the comments above, just print the random value, without iterating on anything:
import random
from_num = int(input('Generate a random number:nFrom:'))
to_num = int(input('To:'))
if from_num > to_num:
print('You did not entered valid min/max numbers')
return
random_num = random.randrange(from_num,to_num+1):
print(f'Random number: {random_num}')
Following to the comments above, just print the random value, without iterating on anything:
import random
from_num = int(input('Generate a random number:nFrom:'))
to_num = int(input('To:'))
if from_num > to_num:
print('You did not entered valid min/max numbers')
return
random_num = random.randrange(from_num,to_num+1):
print(f'Random number: {random_num}')
edited Dec 29 '18 at 19:08
answered Dec 29 '18 at 19:01
ItayBItayB
3,07042644
3,07042644
This is not a complete solution as it drops the range check in the original example.
– Jason Baumgartner
Dec 29 '18 at 19:05
1
@JasonBaumgartner fixed, thanks
– ItayB
Dec 29 '18 at 19:09
add a comment |
This is not a complete solution as it drops the range check in the original example.
– Jason Baumgartner
Dec 29 '18 at 19:05
1
@JasonBaumgartner fixed, thanks
– ItayB
Dec 29 '18 at 19:09
This is not a complete solution as it drops the range check in the original example.
– Jason Baumgartner
Dec 29 '18 at 19:05
This is not a complete solution as it drops the range check in the original example.
– Jason Baumgartner
Dec 29 '18 at 19:05
1
1
@JasonBaumgartner fixed, thanks
– ItayB
Dec 29 '18 at 19:09
@JasonBaumgartner fixed, thanks
– ItayB
Dec 29 '18 at 19:09
add a comment |
Replace this :
for num in range(random.randrange(from_num,to_num+1)):
if True:
print(f'Random number: {num}')
else:
print('You did not entered valid min/max numbers')
with :
ran_num = random.randint(from_num,to_num)
print("Random number is " + str(ran_num))
add a comment |
Replace this :
for num in range(random.randrange(from_num,to_num+1)):
if True:
print(f'Random number: {num}')
else:
print('You did not entered valid min/max numbers')
with :
ran_num = random.randint(from_num,to_num)
print("Random number is " + str(ran_num))
add a comment |
Replace this :
for num in range(random.randrange(from_num,to_num+1)):
if True:
print(f'Random number: {num}')
else:
print('You did not entered valid min/max numbers')
with :
ran_num = random.randint(from_num,to_num)
print("Random number is " + str(ran_num))
Replace this :
for num in range(random.randrange(from_num,to_num+1)):
if True:
print(f'Random number: {num}')
else:
print('You did not entered valid min/max numbers')
with :
ran_num = random.randint(from_num,to_num)
print("Random number is " + str(ran_num))
answered Dec 29 '18 at 19:02
anirudhanirudh
113
113
add a comment |
add a comment |
Why do you have a loop?
import random
from_num = int(input('Generate a random number:nFrom:'))
to_num = int(input('To:'))
if to_num > from_num:
ran_number = random.randrange(from_num,to_num+1)
print(f'Random number: {ran_number}')
else:
print('You did not entered valid min/max numbers')
add a comment |
Why do you have a loop?
import random
from_num = int(input('Generate a random number:nFrom:'))
to_num = int(input('To:'))
if to_num > from_num:
ran_number = random.randrange(from_num,to_num+1)
print(f'Random number: {ran_number}')
else:
print('You did not entered valid min/max numbers')
add a comment |
Why do you have a loop?
import random
from_num = int(input('Generate a random number:nFrom:'))
to_num = int(input('To:'))
if to_num > from_num:
ran_number = random.randrange(from_num,to_num+1)
print(f'Random number: {ran_number}')
else:
print('You did not entered valid min/max numbers')
Why do you have a loop?
import random
from_num = int(input('Generate a random number:nFrom:'))
to_num = int(input('To:'))
if to_num > from_num:
ran_number = random.randrange(from_num,to_num+1)
print(f'Random number: {ran_number}')
else:
print('You did not entered valid min/max numbers')
answered Dec 29 '18 at 19:04
Jason BaumgartnerJason Baumgartner
292110
292110
add a comment |
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%2f53972483%2fcant-make-this-random-number-generator-to-work-properly%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
3
If you only want a single value, why on earth do you have a for loop?
– jonrsharpe
Dec 29 '18 at 18:58
1
Or on any planet for that matter.
– Paritosh Singh
Dec 29 '18 at 19:01
1
Also, your
else
clause will never get executed withTrue
as your if statement condition.– busybear
Dec 29 '18 at 19:02