Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Ernest Nežić
1,839 PointsI have trouble in making my Python program,please help as soon as posible tnx!
Here is program,I want someone to type 2 numbers and than program asks him:"Do you want decimal numbers?"If anwser is yes than program types decimal number betwen those 2 numers,if no program types full number betwen those 2 numbers,I have problem in input,please help!Tnx!:D XD
print('Program for giving random nuber')
a=float(input('Enter the first number:'))
b=float(input('Enter the secound number:'))
c=str(input('Do you want decimal numbers?:'))
if c=='ne' or c=='NE' or c=='Ne' or c=='nE':
while input==chr(13):
import random
n=random.randint(a,b)
print(n)
elif c=='da' or c=='DA' or c=='Da' or c=='dA':
while input==chr(13):
import random
m=random.uniform(a,b)
print(m)
else:
print('The input word is not corect!')
2 Answers
Oziel Perez
61,321 PointsI'm not exactly sure if I understood your question correctly. What I understood was that you want to generate a random number between number a and number b and you want to give the user an option of if you wish to leave the generated number as a decimal or turn into a whole integer, right? If so, here's how I did it:
import random;
print('Program for giving random nuber ');
a = float(input('Enter the first number: '));
b = float(input('Enter the secound number: '));
c = str(input('Do you want decimal numbers?: ')).lower();
n = 0;
while n < a:
n = random.random() * (b - a) + a;
if c=='no':
n = round(n);
elif c=='yes':
pass;
else:
print('The input word is not corect!');
print(n);
Before anything, you will notice I refactored your code so that it's easier to read and works more efficiently. I start by using "import random" to ensure that we do our imports at the top. I added .lower() to the input string that's assigned to the variable c; this is just so that you don't have to use "c == 'yes' OR c == 'Yes'" down at the bottom; it's easier. Then I use random.random() so that we can generate an actual floating point number from scratch. This generates a decimal between 0 and 1 (e.g. 0.4567363763). To set it to a number between the inputted numbers, the formula is to multiply by the result of b - a, which is our range of numbers. Example: say we have a = 3 and b = 10. Our range is 7 because that's how many whole integers we can generate in total in that range. It's like amplifying the scale from 0 - 1 to 0 - 7. Then just add the variable a to that result to ensure that it is at least "a", or 3 in the example, and at most b, or 10. Lastly, just check to see if the user said yes or no to the decimals. If "no", the random number is rounded up or down depending on the decimal, and assigned back to the original n variable. If yes, it just skips this process altogether. If any other word, it will print the error and loop back around, asking again for an input of yes or no.
Note that random.random() is inclusive, which means a and be can be selected as the numbers. If you want to exclude a and be, just tweak the formula like this: random.random() * ((b - 1) - (a + 1)) + (a + 1)
Chris Freeman
Treehouse Moderator 68,468 PointsThe while statements are not needed and should be removed. "input" is a function and will never match chr(13) or a "\r".
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 Pointsfixed code formatting