Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Recently, I started programming in python, right now I'm writing a GUI for a password and pseudo-random number generator. I had this problem:
TypeError: 'PySide6.QtWidgets.QLineEdit' object is not iterable
btn_2 = QPushButton("Generate password", self)
btn_2.resize(100,30)
btn_2.move(340, 250)
btn_2.resize(100, 30)
btn_2.clicked.connect(self.btn1)
btn_3 = QPushButton("Generate pseudorandom numbers", self)
btn_3.move(140, 250)
btn_3.resize(180, 30)
btn_3.clicked.connect(self.btn2)
self.line = QLineEdit(self)
self.line.setPlaceholderText("Enter the expected string length")
self.line.resize(250, 20)
self.line.move(200, 220)
self.onlyInt = QIntValidator()
self.line.setValidator(self.onlyInt)
self.show()
def btn1(self):
mark = self.line
chars = string.ascii_letters + string.digits
print("".join(random.choice(chars) for _ in mark))
def btn2(self):
mark = self.line
chars = string.digits
print("".join(random.choice(chars) for _ in mark))
use self.line.text rather than self.line.
So:
mark = int(self.line.text)
chars = string.ascii_letters + string.digits
print("".join(random.choice(chars) for _ in range(mark))
Note that I've also replaced mark with range(mark) which is probably what you want here.
In general when given an XYZ isn't iterable error, go and look at the docs for XYZ and see how to get at whatever it is storing and you want to iterate over. In this case we see that we can get at the text which is presumably what you want with .text. But as it happens I think you want to iterate self.line.text times (hence your checking for an int), so we cast to int and then iterate through range(int(self.line.text)).
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have a simple code to get user email information, I got a problem about something :
import sys
try:
argx = sys.argv(1)
except:
print("Please Input an email.")
Example For User argument :
py main.py example#example.com
I want to take arg (input) after the # char and the . char.
I need this to check the email provider, domain name and other stuff about the email.
Example of the thing i want :
import sys
try:
argx = sys.argv(1)
x = "The argument after the char #, and the ."
except:
print("Please Input an email.")
if(x.lower() == "gmail") :
gmail()
elif(x.lower == "protonmail") :
protonmail()
You can use split function of a string in order to split a string
like
x = 'name#example.com' #Your input
email = x.split('#') # will give ['name','example.com']
provider = email[1].split('.')[0] # will give 'example'
Asuming you have the following email example#example.com. In python you can split a string with the split function. The function itself needs a delimeter. In your case the delimeter would be #. The return value of the split function is an array.
parts_of_mail = email.split("#")
>>> [example, example.com]
You now have the array parts_of_mail, which stores the left and right part of the email. You can now split the string again, like above:
provider_info = parts_of_mail[1].split(".")
>>> [example, com]
Finally you can check the provider information:
if provider_info[0].lower() == "example":
# do stuff here
elif provider_info[0].lower() == "gmail":
# do stuff here
Note: provider_info[0] stores the provider and provider_info[1] stores the domain.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm making a simple program in order to compare the currentdate to another date, and I need to separate the "/" and remove trailing zeros; I made a function for that:
def correctdate(compare, Dayslist, Monthslist):
for day in compare:
if day == "/" or day == "\\":
compare.remove(day)
break
else:
Dayslist.append(day)
for removenum in Dayslist:
#* Removing the numbers from the list
compare.remove(removenum)
for month in compare:
Monthslist.append(month)
#* Joining the numbers into a string
correctdate.DaysLeft = "".join(Dayslist)
correctdate.MonthsLeft = "".join(Monthslist)
#* Stripping leading zeros
correctdate.DaysLeft = correctdate.DaysLeft.lstrip("0")
correctdate.MonthsLeft = correctdate.MonthsLeft.lstrip("0")
return
The code works just fine but i want to save the DaysLeft, Monthsleft to print it/edit it ETC..
so i do this:
correctdate(compare,Dayslist,Monthslist)
print(correctdate.Daysleft)
and i get this:
AttributeError: 'function' object has no attribute 'Daysleft'
There was a typo in printing the object attribute
I wrote:
print(correctdate.Daysleft)
Its supposed to be:
print(correctdate.DaysLeft)
Sorry for the inconvenience
You ve to return it in your function, and outside it get it into variables:
def correctdate(compare, Dayslist, Monthslist):
for day in compare:
if day == "/" or day == "\\":
compare.remove(day)
break
else:
Dayslist.append(day)
for removenum in Dayslist:
#* Removing the numbers from the list
compare.remove(removenum)
for month in compare:
Monthslist.append(month)
#* Joining the numbers into a string
correctdate.DaysLeft = "".join(Dayslist)
correctdate.MonthsLeft = "".join(Monthslist)
#* Stripping leading zeros
correctdate.DaysLeft = correctdate.DaysLeft.lstrip("0")
correctdate.MonthsLeft = correctdate.MonthsLeft.lstrip("0")
return correctdate.DaysLeft,correctdate.MonthsLeft
This for return outside function, now you ve to call function correctly:
DaysLeft,Monthsleft = correctdate(compare,Dayslist,Monthslist)
print(DaysLeft,Monthsleft)
Anyway this code "correctdate.MonthsLeft" looks like you want use class and not only functions, so you should use like that https://www.w3schools.com/python/python_classes.asp
def correctdate(compare, Dayslist, Monthslist):
You declare a function with name correctdate that accepts 3 parameters.
correctdate.DaysLeft = "".join(Dayslist)
correctdate.MonthsLeft = "".join(Monthslist)
Then you try to assign a value to a function, which is not possible because correctdate is not a variable, not an object. You just declared it as a function.
What are you trying to achieve?
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm having the below code which is really simple, but I just can't figure out why I'm getting the above error. I can't seem to call the function defined above, as it is a boolean object??
def server(jobs):
# master clock jumps to next job
global master
master = jobs[0][0]
jobs = [(1, 2.1),(2, 3.3),(3, 1.1),(5, 0.5),(15, 1.7)]
# master clock
master = 0
# server = true when busy, server = false when idle
server = False
next_arrival = jobs[0][0]
# assuming no future arrivals
next_departure = np.inf
job_list = []
print("master clock: " + str(master) + ", next arrival time: " + str(next_arrival) + ", next departure time: " + str(next_departure) + ", job list: " + str(job_list))
server(jobs)
You shadowed your function name with a bool variable when you wrote server = false. You can't have a function and a variable with the same name in the same scope.
Name that variable or the function something else, because False() doesn't make any sense.
You just need to change the name of the function. Change the function name from server to server_new and it will work.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I try to write a program and get a variable from a constructor to a method but I don't know how to do it :( I get an error with p.liczba in the method sprawdz. I would like to have here a number from the user. The same situation is with p.wielokrotnosc. I know that I can write this program easier but I really would like to learn OOP with simple examples like this
class Przyklad:
def __init__(self, liczba):
self.liczba = liczba
def podajSzukana(self):
self.wielokrotnosc = 3
class Dane:
def wczytaj(self):
a = int(input('Podaj mi liczbę '))
p = Przyklad(a)
def sprawdz(self):
if (p.liczba % p.wielokrotnosc == 0):
return true
print ('Witaj w programie szukającym wielokrotność liczby 3 \n')
d = Dane()
d.wczytaj()
d.sprawdz()
The problem is not getting the variable from the constructor of Przyklad. The problem is saving it in Dane. The common procedure is to attach it to instance of Dane:
def wczytaj(self):
a = int(input('Podaj mi liczbę '))
self.p = Przyklad(a)
Then, you'll have self.p available in Dane
This is happening because the variable liczba is contained within the class Przyklad, so when you try to access it in Dane, it is not possible.
You should try having liczba be a global variable (which is dangerous), or having Dane be a descendant of Przyklad (these names though....).
a and p are local variables not members of Dane. You have to use self. (as pointed by jonrsharpe) to be able to access this variable from the class context:
class Dane:
def wczytaj(self):
a = int(input('Podaj mi liczbę '))
self.p = Przyklad(a)
def sprawdz(self):
if (self.p.liczba % self.p.wielokrotnosc == 0):
return true
Another issue is that self.wielokrotnosc do not exists until you call podajSzukana(self) method. One way to fix this is calling that function inside your constructor:
class Przyklad:
def __init__(self, liczba):
self.liczba = liczba
self.podajSzukana() // Call the method here.
def podajSzukana(self):
self.wielokrotnosc = 3
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am getting an invalid syntax error
SyntaxError: invalid syntax
root#collabnet:/home/projects/twitterBot# python twitterBot2.py
File "twitterBot2.py", line 58
return screenNames
when returning a dictionary from this function:
def getUserName(lookupIds):
l = len(lookupIds) # length of list to process
i = 0 #setting up increment for while loop
screenNames = {}#output dictionary
count = 0 #count of total numbers processed
print 'fetching usernames'
while i < l:
toGet = []
toAppend = []
if l - count > 100:#blocks off in chunks of 100
for m in range (0,100):
toGet.append(lookupIds[count])
count = count + 1
print toGet
else:#handles the remainder
print 'last run'
r = l - count
print screenNames
for k in range (0,r):#takes the remainder of the numbers
toGet.append(lookupIds[count])
count = count + 1
i = l # kills loop
toAppend = api.lookup_users(user_ids=toGet)
print toAppend
screenNames.append(zip(toGet, toAppend)
#creates a dictionary screenNames{user_Ids, screen_Names}
#This logic structure breaks up the list of numbers in chunks of 100 or their
#Remainder and addes them into a dictionary with their count number as the
#index value
#print str(len(toGet)), 'screen names correlated'
return screenNames
I am running the function like so:
toPrint = {}#Testing Only
print "users following", userid
toPrint = getUserName(followingids)#Testing Only
I have tried commenting out and just printing screenNamesand I still get the same error except on the print statement instead. I am pretty sure I am running the return right thanks for the look.
You forgot a closing parenthesis on a preceding line:
screenNames.append(zip(toGet, toAppend)
# ^ ^ ^^?
# | \---- closed ---/|
# \----- not closed ---/
You'll have another problem here, as screenNames is a dict object, not a list, and has no .append() method. If you wanted to update the dictionary with key-value pairs, use update() instead:
screenNames.update(zip(toGet, toAppend))