0

I just want to generate random float numbers between the range.But i am facing some issue This is my code

import xlsxwriter
import random

wb = xlsxwriter.Workbook('abc.xlsx')

ws = wb.add_worksheet()

start = input("Enter the start value:")
end = input("Enter the end value:")
ws.write('A1','InputRPM')

ws.write('A3',start)

row=3
col=0 

rand = [random.uniform(start,end) for i in range(20)]

rand.sort()

for i in rand:
  ws.write(row,col, i)
  row+=1

ws.write('A24',end)
wb.close()

Error:

Enter the start value:200
Enter the end value:300
Traceback (most recent call last):
  File "file.py", line 17, in <module>
    rand = [random.uniform(start,end) for i in range(20)]
  File "file.py", line 17, in <listcomp>
    rand = [random.uniform(start,end) for i in range(20)]
  File "/usr/lib/python3.8/random.py", line 417, in uniform
    return a + (b-a) * self.random()
TypeError: unsupported operand type(s) for -: 'str' and 'str'

I want to print generated float numbers in excel without using range in for loop.It will generate automatically once it reach the end value it stops the generating process.I am a new guy for this so i don't know the things.Can anyone help me to find this requirement.

3
  • Your problem is that start and end are strings, as input always returns a string. You probably want start = float(input("Enter the start value:")), and the same for end. Commented Sep 17, 2021 at 5:48
  • The input() function returns a string not an int, you need to pass to random.uniform a float/int value. Commented Sep 17, 2021 at 5:49
  • Thankyou bro its working.Can you know for this "I want to print generated float numbers in excel without using range in for loop.It will generate automatically once it reach the end value it stops the generating process" Commented Sep 17, 2021 at 5:51

1 Answer 1

0

Your input type is 'str', you need to change it to 'int' or 'float' for using in random function. Output will be still float number. Try this;

start = int(input("Enter the start value:"))
end = int(input("Enter the end value:"))
Sign up to request clarification or add additional context in comments.

4 Comments

Thankyou bro its working fine only.Can you know for this "I want to print generated float numbers in excel without using range in (for loop).It will generate automatically once it reach the end value it stops the generating process"
I don't fully understand what you want. How do you want to determine how many characters to create? Maybe you can use while func. You can stop when it's above a certain value.
Sorry for the confusion.I just I want to write the float numbers in excel using only start and end value.these two are my inputs.I won't give any range(How much random numbers for generating). It will generate automatically.I want inbetween all float numbers can you know how to print...?
There are infinite float numbers between two numbers. Maybe you can try to choose range number randomly and everytime you can print totaly random numbers. random_range = random.randrange(1000) rand = [random.uniform(start,end) for i in range(random_range)]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.