Reputation: 65
Alright so my guidelines are:
I am having trouble with x
not being defined. I have added ellipses to help condense a bit. So the program is:
import random
import math
def randstr():
global x
int(x=0)
a = ('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
rs = ((random.choice(a)) + (random.choice(a)) + (random.choice(a)) + (random.choice(a)) + (random.choice(a)) + (random.choice(a)) + (random.choice(a)) + (random.choice(a)) + (random.choice(a)) + (random.choice(a)))
print rs
if rs[0] == 'A' or 'B' or 'C' or 'D' or 'E' or 'F' or 'G' or 'H' or 'I' or 'J' or 'K' or 'L' or 'M' or 'N' or 'O' or 'P' or 'Q' or 'R' or 'S' or 'T' or 'U' or 'V' or 'W' or 'X' or 'Y' or 'Z':
x += 1
if rs[0] == 'a' or 'b' or 'c' or 'd' or 'e' or 'f' or 'g' or 'h' or 'i' or 'j' or 'k' or 'l' or 'm' or 'n' or 'o' or 'p' or 'q' or 'r' or 's' or 't' or 'u' or 'v' or 'w' or 'x' or 'y' or 'z':
x += 1
elif rs[1] == 'a' or 'b' or 'c' or 'd' or 'e' or 'f' or 'g' or 'h' or 'i' or 'j' or 'k' or 'l' or 'm' or 'n' or 'o' or 'p' or 'q' or 'r' or 's' or 't' or 'u' or 'v' or 'w' or 'x' or 'y' or 'z':
x += 1
...
elif rs[9] == 'a' or 'b' or 'c' or 'd' or 'e' or 'f' or 'g' or 'h' or 'i' or 'j' or 'k' or 'l' or 'm' or 'n' or 'o' or 'p' or 'q' or 'r' or 's' or 't' or 'u' or 'v' or 'w' or 'x' or 'y' or 'z':
x += 1
if rs[1] == 'A' or 'B' or 'C' or 'D' or 'E' or 'F' or 'G' or 'H' or 'I' or 'J' or 'K' or 'L' or 'M' or 'N' or 'O' or 'P' or 'Q' or 'R' or 'S' or 'T' or 'U' or 'V' or 'W' or 'X' or 'Y' or 'Z':
x -= 2
...
elif rs[9] == 'A' or 'B' or 'C' or 'D' or 'E' or 'F' or 'G' or 'H' or 'I' or 'J' or 'K' or 'L' or 'M' or 'N' or 'O' or 'P' or 'Q' or 'R' or 'S' or 'T' or 'U' or 'V' or 'W' or 'X' or 'Y' or 'Z':
x -= 2
if rs[0] == '1' or '2' or '3' or '4' or '5' or '6' or '7' or '8' or '9' or '0':
x -= 2
if rs [1] == 'a' or 'b' or 'c' or 'd' or 'e' or 'f' or 'g' or 'h' or 'i' or 'j' or 'k' or 'l' or 'm' or 'n' or 'o' or 'p' or 'q' or 'r' or 's' or 't' or 'u' or 'v' or 'w' or 'x' or 'y' or 'z' or 'A' or 'B' or 'C' or 'D' or 'E' or 'F' or 'G' or 'H' or 'I' or 'J' or 'K' or 'L' or 'M' or 'N' or 'O' or 'P' or 'Q' or 'R' or 'S' or 'T' or 'U' or 'V' or 'W' or 'X' or 'Y' or 'Z':
x -= .25
...
elif rs [9] == 'a' or 'b' or 'c' or 'd' or 'e' or 'f' or 'g' or 'h' or 'i' or 'j' or 'k' or 'l' or 'm' or 'n' or 'o' or 'p' or 'q' or 'r' or 's' or 't' or 'u' or 'v' or 'w' or 'x' or 'y' or 'z' or 'A' or 'B' or 'C' or 'D' or 'E' or 'F' or 'G' or 'H' or 'I' or 'J' or 'K' or 'L' or 'M' or 'N' or 'O' or 'P' or 'Q' or 'R' or 'S' or 'T' or 'U' or 'V' or 'W' or 'X' or 'Y' or 'Z':
x -= .25
...
elif rs [8] == 'a' or 'b' or 'c' or 'd' or 'e' or 'f' or 'g' or 'h' or 'i' or 'j' or 'k' or 'l' or 'm' or 'n' or 'o' or 'p' or 'q' or 'r' or 's' or 't' or 'u' or 'v' or 'w' or 'x' or 'y' or 'z' or 'A' or 'B' or 'C' or 'D' or 'E' or 'F' or 'G' or 'H' or 'I' or 'J' or 'K' or 'L' or 'M' or 'N' or 'O' or 'P' or 'Q' or 'R' or 'S' or 'T' or 'U' or 'V' or 'W' or 'X' or 'Y' or 'Z':
x -= .25
return rs
print randstr()
randstr()
Upvotes: 1
Views: 1351
Reputation: 498
So if you were to add an x=0
or just x=int(0)
at the beginning of your code it would work fine, because you are attempting to define x=
as an int()
which is not possible.
import random
import math
def randstr():
# I removed the 'global x' you should not need it
x=0 # I made the change for you
a = ('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
This should fix your problem of 'x not defined' and you should also be able to get rid of the global x
Upvotes: 0
Reputation: 5673
There are some other errors you need to notice before moving forward.
First problem is in your if
statements.
Take a look at this example:
string = "foo"
if string == "foo" or "bar":
dosomething()
To the human eye, that looks correct, but the computer sees it differently. When you are combining conditions with and
or or
, the computer checks the conditions left to right. So in this case, the computer sees:
If string is equal to foo or if bar
The second condition, after the or
is the same as saying if bar == True:
Bar is a string, and any string is always true, so the dosomething()
function will always execute. This can be fixed by changing the above example to:
string = "foo"
if string == "foo" or string == "bar":
dosomething()
Now the computer will execute it as:
If string is equal to foo or if string is equal to bar
Simplifying your code
The second thing I want to point out is one of the number one rules in programming: if you're typing the same thing multiple times, there is probably a better way
In your code, imagine replacing the already long conditions with if rs[9] == 'A' or rs[9] == 'B'
, etc. It's going to get extremely long and messy. It would be much simpler to make a list of the alphabet, and check your variable against the list. For example:
alpha_list = ['A','B','C'] # etc through 'Z'
if rs[0] in alpha_list:
dosomething()
The in
keyword tells Python to check if the value of rs[0]
is in the list alpha_list, or in other words, if it is a letter in the alphabet. Now you can replace all those long messy conditionals with a much shorter, cleaner solution :)
As aIKid pointed out, a string is also an iterable so you could replace the list with a string of all the letters (alphabet = "ABCDEF"
...). Either one would work.
As for the original answer to your question, aIKid answered it perfectly, I just figured I'd try to shed some light on some other ways to help you improve!
Upvotes: 2
Reputation: 28242
Instead of int(x=0)
- calling a function - you have to declare it:
x = 0
Also, you don't seem to need to have the global x
in your function.
So, here is a part of your function:
def randstr()
x = 0
#some more code
#...
return rs
And there's still a couple more problems.
First, the declaration of rs
. What you're using is +
, but you should use ,
, or else python will just sum all of the values, and returns a number. Remove the unnecessary brackets as well:
rs = [random.choice(a), random.choice(a), ...]
Or just use a loop:
rs = []
for i in range(10): #creating 10 random numbers
rs.append(random.choice(a))
Your if
statement also doesn't do what you think it does. Use the in
operator instead:
if rs[0] in 'abcdefghijklmnopqrstuvwxyz':
...
And similarly for other conditional statements. Hope this helps!
Upvotes: 3