im makin a machine for prime number,But I want the output of the numbers to be stuck together and horizontal, for example:
>>2,3,5,7,11,13,......
but machine do this:
It is neither horizontal nor joined together
my code:
from tkinter import *
def prime():
a = int(spin.get())
for number in range(a + 1):
if number > 1:
for i in range(2,number):
if (number % i) == 0:
break
else:
Label(app,text=number,).pack()
app = Tk()
app.maxsize(300,300)
app.minsize(300,300)
spin = Entry(app,font=20)
spin.pack()
Button(app,text="prime numbers",command=prime).pack()
app.mainloop()
You can do this by storing the numbers in a list (result) and then joining them at the end.
def prime():
a = int(spin.get())
result = []
for number in range(a + 1):
if number > 1:
for i in range(2,number):
if (number % i) == 0:
break
else:
result.append(str(number))
Label(app, text = ",".join(result)).pack()
The numbers have to be stored as strings so join will work.
Edit
To wrap every 10 numbers, add the following in place of Label(app, ...) after the for loop:
for i in range(0, len(result), 10):
Label(app, text = ",".join(result[i:i+10])).pack()
Related
I am building a machine that can recognize prime numbers individually and show the result to the user,But there is a problem that I showed in the text below
>>12
>>12,is a not prime number
>>7
>>7,is a not prime number
Which prime number and which composite number can be converted into composite numbers
my codes:
from tkinter import *
def rso():
a = int(text.get())
for i in range(a + 1):
pass
if a % i == 1:
Label(app,text=(a,"is a prime number"),font=20).pack()
else:
Label(app,text=(a,"is a NOT prime number"),font=20).pack()
app = Tk()
text = Entry(app,font=20)
text.pack()
Button(app,text="sumbit",font=20,command=rso).pack()
app.mainloop()
Try this:
num = 3
flag = False
if num > 1:
# check for factors
for i in range(2, num):
if (num % i) == 0:
flag = True
break
if flag:
print(f"{num} is not a prime number")
else:
print(f"{num} is a prime number")
import tkinter as tk
import math
window = tk.Tk()
label = tk.Label(text = 'Want to find a root?')
label.pack()
entry = tk.Entry(fg = 'blue')
entry.pack()
n = entry.get()
number = int(n)
answers = {}
roots = [x for x in range(2, 100)]
def search(number):
for i in roots:
if number > i:
if number//i**2 != 0:
if number//i**2 != 1:
if (i**2)*(number//i**2) == number:
answers[i] = number//i**2
print(answers)
search(number)
window.mainloop()
So I need to get a integer from entry and work with it as a int, but entry gives me a string with which i can't work.I can't type a int in entry because the programm doesn't start due to an error
Error:number = int(n)
ValueError: invalid literal for int() with base 10: ''
You are getting the value of an Entry you created seconds ago. Of course it will be nothing! Use entry.get() when you need it, not anytime before. Put the .get() in the search function, and then call int on it.
Updated search function:
def search(number):
number = int(entry.get())
for i in roots:
if number > i:
if number//i**2 != 0:
if number//i**2 != 1:
if (i**2)*(number//i**2) == number:
answers[i] = number//i**2
And remove the two lines in your code that get the entry value and turn it into an int.
I'm new to programming and I'm trying to make an app that calculates the smallest common for you, but for some reason whenever i run it, tkinter seems to just freeze and i don't know what the error seems to be. I suspect that it's the myLabel part, since I can still read the result inside the Terminal.
Thanks in advance~
from tkinter import *
root = Tk()
root.title("I can find the smallest common, unless you enter letters... I'm dyslexic.")
numbers_list = []
global numbers
numbers = Entry(root, width = 10, borderwidth = 5, state = DISABLED)
numbers.pack()
numbers.insert(0, "")
#
def button_click():
#each gets a button
get_smallest_common = Button(root, text = 'Confirm your number!'
, command = smallest_common)
get_smallest_common.pack()
get_smallest_common = Button(root, text = 'Undo!'
, command = lambda: undo())
get_smallest_common.pack()
get_smallest_common = Button(root, text = 'Start the search!'
, command = lambda: find_the_s_common())
get_smallest_common.pack()
#disable the start button
def switch():
myButton['state'] = DISABLED
#configure helps bringing a disabled thing back to normal state
numbers.configure(state = "normal")
def smallest_common():
#add to the list for late use
numbers_list.append(numbers.get())
print(numbers_list)
numbers.delete(0, END)
def undo():
#add to the list for late use
numbers_list.pop()
print(numbers_list)
numbers.delete(0, END)
def find_the_s_common():
process_list = []
condition = True
x = 0
while condition:
#the multiplication keep rising till count is 3
a = int(x) + 1
x = a
#loop to multiply the number with x + 1
for number in numbers_list:
y = int(number) * int(a)
process_list.append(y)
#check whether the result has been added to append into list
if y in process_list:
#check whether the list contains two duplicates to
if process_list.count(y) == 3:
condition = False
result = 'The number is ' + str(y) + '!'
print(result)
else:
continue
else:
continue
myLabel = Label(root, text = result)
myLabel.pack()
#combine the two function for myButton
def button_click_switch():
button_click()
switch()
myButton = Button(root, text = 'Click me to start'
, command = lambda: [button_click(), switch()])
myButton.pack()
root.mainloop()
Most probably, you have problem if the numbers entered is less than 3. As a result, one simple way is to decrease the condition to 2 and if a single number is entered the result is the number itself, if 2 or more no freezing.
NOTE: Freezing here is actually, while loop is running as condition is always True, because of process_list.count(y) == 3 will always be False if less than 3 number entered.
See my suggestion:
from tkinter import *
root = Tk()
root.title("I can find the smallest common, unless you enter letters... I'm dyslexic.")
numbers_list = []
global numbers
numbers = Entry(root, width = 10, borderwidth = 5, state = DISABLED)
numbers.pack()
numbers.insert(0, "")
#
def button_click():
#each gets a button
get_smallest_common = Button(root, text = 'Confirm your number!', command = smallest_common)
get_smallest_common.pack()
get_smallest_common = Button(root, text = 'Undo!', command = lambda: undo())
get_smallest_common.pack()
get_smallest_common = Button(root, text = 'Start the search!' , command = lambda: find_the_s_common())
get_smallest_common.pack()
#disable the start button
def switch():
myButton['state'] = DISABLED
#configure helps bringing a disabled thing back to normal state
numbers.configure(state = "normal")
def smallest_common():
#add to the list for late use
numbers_list.append(numbers.get())
print(numbers_list)
numbers.delete(0, END)
def undo():
#add to the list for late use
numbers_list.pop()
print(numbers_list)
numbers.delete(0, END)
def find_the_s_common():
process_list = []
condition = True
x = 0
if len(numbers_list) == 1: # RETURN IF THE LIST HAS A SINGLE VALUE
result = 'The number is ' + str(numbers_list[0]) + '!'
else:
while condition:
#the multiplication keep rising till count is 3
a = int(x) + 1
x = a
#loop to multiply the number with x + 1
for number in numbers_list:
y = int(number) * int(a)
process_list.append(y)
#check whether the result has been added to append into list
if y in process_list:
#check whether the list contains two duplicates to
if process_list.count(y) == 2: # DECREASE THE CONDITIONS TO 2
condition = False
result = 'The number is ' + str(y) + '!'
print(result)
else:
continue
else:
continue
myLabel = Label(root, text = result)
myLabel.pack()
#combine the two function for myButton
def button_click_switch():
button_click()
switch()
myButton = Button(root, text = 'Click me to start', command = lambda: [button_click(), switch()])
myButton.pack()
root.mainloop()
Suggestion to add two conditons;
(1) create a condition until at least 1 number is entered! if len(numbers_list) == 0, loop to enter a number.
(2) create a condition, until N number is added, let's say user must enter at least 3 numbers, change process_list.count(y) == 3 to process_list.count(y) == N and add condition if len(numbers_list) != N, loop to enter more numbers.
I use this code in python37 and it generates numbers in the list, but I would also like it to create this name along with it for the list name.
The code just does this:
[1,2,3,4]
This is the name I hoping for to add it to it on the fly:
lst1z
Here is the result for what I hope someone can edit the code to make it do this:
lst1z = [1,2,3,4]
So here is my code Thanks:
composites=[]
n = int(input("Enter any number : "))
positive_n = abs(n)
for num in range(positive_n+1):
if num > 1:
for i in range(1, num,1):
if (num % i) == 0:
if n > 0:
composites.append(num)
else:
composites.append(-num)
break
print ("\ncomposites from", int(positive_n / n), " to ", n, "\n", composites)
composites=[]
n = int(input("Enter any number : "))
positive_n = abs(n)
for i,num in enumerate(range(positive_n+1)):
if num > 1:
for i in range(1, num,1):
if (num % i) == 0:
if n > 0:
composites.append(num)
else:
composites.append(-num)
break
#If all you need is to create vairalble and print, you can simply do it as follows:
list1z = composites
print("\ncomposites from {} to {} \n list1z = {}".format(int(positive_n/n), n, list1z))
print ("\ncomposites from", int(positive_n / n), " to ", n, "\n", composites)
So, I'm quite nooby at python. I decided to make a program that makes prime numbers. I know there's probably a function built in that does this but I decided to do it myself.
number = 1
numlist = list()
for x in range (0, 1000):
numlist.append("")
print "Created list entry " + str(x)
while True:
number = number + 1
if number % 2 != 0:
numscrollerA = 1
numscrollerB = 1
while numscrollerA <= number:
if float(number) / float(numscrollerA) == float(int(number)):
numlist[numscrollerA] = "true"
if float(number) / float(numscrollerA) != float(int(number)):
numlist[numscrollerA] = "false"
numscrollerA = numscrollerA + 1
while numscrollerB <= number:
if numscrollerB != 1 and numscroller != number and numlist[numscrollerB] == "true":
primestatus = "false"
else:
primestatus = "true"
if primestatus == "true":
print number
I get "Created list entry x" 1000 times as I should. Then the program just hangs.
while numscrollerB <= number:
if numscrollerB != 1 and numscroller != number and numlist[numscrollerB] == "true":
primestatus = "false"
else:
primestatus = "true"
You don't increase numscrollerB in this loop, so it runs infinitedly. Anyway, You should rather use 'for loop':
for numscrollerB in range(1, number+1):
pass # do something
Your code is very unpythonic. Typical of a newcomer experienced in a different style of coding.
Your list is uneccessary.
In python you could create the list like this
def check_even(val):
#this contains your logic
return val % 2 == 0
evenslist = [check_even(i) for i in xrange(1, 1001)]
print numlist