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.

  • 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`. – Tim Roberts Sep 17 '21 at 05:48
  • The `input()` function returns a `string` not an `int`, you need to pass to `random.uniform` a `float`/`int` value. – SAL Sep 17 '21 at 05: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" – Someshwaran Sep 17 '21 at 05:51

1 Answers1

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:"))
Femur
  • 16
  • 3
  • 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" – Someshwaran Sep 17 '21 at 12:16
  • 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. – Femur Sep 17 '21 at 12:40
  • 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...? – Someshwaran Sep 18 '21 at 10:00
  • 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)]` – Femur Sep 19 '21 at 13:34