is there any reasonable way to convert my while
loop into a for
loop? I tried to create this, but I never got far cause the range
's and/or xrange
's broke because the number was too large or they don't take a float
. Here's my while
loop that I want to see if there is a reasonable for
loop conversion for(This is within a function is why it's indented):
a = float(E1.get())
b = (a + 1)
c = sqrt(b)
e = sqrt(a)
f = (unichr(0x221A))
fractional, integral = modf(c)
while integral == integral:
fractional1, integral1 = modf(a / (integral**2))
print(fractional1)
if fractional1 != 0:
integral -= 1
continue
else: break
if a range
or xrange
is possible, it'd have to be range(1,a+1)
or xrange(1,a+1)
This is my entire code:
def radical():
# Define variables needed
a = float(E1.get())
b = (a + 1)
c = sqrt(b)
e = sqrt(a)
f = (unichr(0x221A))
fractional, integral = modf(c)
while integral == integral:
fractional1, integral1 = modf(a / (integral**2))
print(fractional1)
if fractional1 != 0:
integral -= 1
continue
else: break
d = (a / (integral**2))
# make pointless floats integers
if integral.is_integer():
integral = int(integral)
if d.is_integer():
d = int(d)
if e.is_integer():
e = int(e)
# set conditions for output
if d == 1:
global master
master = Tkinter.Tk()
w = Tkinter.Message(master, padx=5, pady=5,text=(integral)).grid(ipadx=5, ipady=5)
master.title("Square Root")
master.mainloop()
elif integral == 1:
master = Tkinter.Tk()
w = Tkinter.Message(master, padx=5, pady=5,text=(f,d,"or",e)).grid(ipadx=5, ipady=5)
master.title("Simplified Radical")
master.mainloop()
else:
master = Tkinter.Tk()
w = Tkinter.Message(master, padx=5, pady=5,text=(integral,f,d,"or",e)).grid(ipadx=5, ipady=5)
master.title("Simplified Radical")
master.mainloop()
If you want infinite loop, you can use itertools.repeat(1)
which will yield infinitely:
import itertools
....
for _ in itertools.repeat(1): # instead of `while integral == integral`
...
You can also use itertools.count()
or itertools.cycle([1])
.
UPDATE
Using range(integral, 0, -1)
, you can iterate from integral
down to 1
(excluding 0).
for integral in range(integral, 0, -1): # xrange if you're using Python 2.x
fractional1, integral1 = modf(a / (integral**2))
print(fractional1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.