When we select a range, the program should generate a random decimal number in the selected range, but it gives an error.

from tkinter import* root=Tk() from random import* def ssg(): if but['text'] == 'Сгенерировать случайное число' and i.get()==2: lab['text']=random(0,1) elif but['text'] == 'Сгенерировать случайное число' and i.get()==3: lab['text']=random(1,100) i=DoubleVar() r4=Radiobutton(root,text='от 0 до 1',variable=i,value=2) r4.grid(row=5,column=0, sticky='w') r5=Radiobutton(root,text='от 1 до 100',variable=i,value=3) r5.grid(row=5,column=3, sticky='w') but=Button(root,text='Сгенерировать случайное число',command=ssg) but.grid(row=3,column=0,columnspan=3) lab=Label(root,text='') lab.grid(row=4,column=0,columnspan=3) 
  • It gives this error because you are trying to set several arguments to the random.random function random.random which in principle does not accept any variables at all, but outputs a random number from 0 to 1. - Twiss

2 answers 2

Here you need to look at the documentation for the library of random for python, here is the version in Russian

If you need any number in the range from A to B, instead of random(A, B) use random.uniform(A, B) or uniform(A, B) in your case

If you need an integer random.randint(A, B)

  • Thank you, you really helped - A.Kross

Because the random () function generates a random number in the range from 0.0 to 1.0. Use the randint () function:

 import random number_one = random.randint(20, 35) print(number_one) #27 number_two = random.random() print(number_two) #0.32161791480941126 
  • Thank. I needed from 0.0 to 1.0, - A.Kross
  • @ A.Kross hmm, why did you try to pass integers to the function arguments? :) - JamesJGoodwin
  • I thought that so I would set the range, I did not know that random works without arguments - A.Kross