1

So I'm a bit of a Python beginner and I am wondering if there is a way to modify or fix this script so that it generates random number/letter sequences. I think I've got the actual generation solved, but I need some help on how to print the result to the console. Here is my code:

def main():
    import random
    random.random() * 10
    myList = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
    random.choice(myList)
    print(random.random() * 10 + random.choice(myList))
main()

Can any of you guys help me out? as I said before, I am a beginner, so it might be a basic mistake and examples in answers would be great.

Error edited in:

line 9, in main
    print(random.random() * 10 + random.choice(myList))
TypeError: unsupported operand type(s) for +: 'float' and 'str'
1
  • 2/5 of your code does nothing: random.random()*10 and random.choice(mylist) - you are calling methods and do not use the returns. What do you want to achieve with those calls? The last print statement will not work, you have to make a string str(93.2923) from your random float first - before you concattenate it with a string by using + Commented Jan 15, 2018 at 19:26

1 Answer 1

1

If you want to print a float, make a string from it:

def main():
    import random
    # random.random() * 10     # does nothing, you do not use the return
    myList = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o",
              "p","q","r","s","t","u","v","w","x","y","z"]
    # random.choice(myList)    # does nothing, you do not use the return
    print(str(random.random() * 10) + random.choice(myList)) 
    # prints a [0.0-1.0[ * 10 + one character

main()

You can benefit from using a constant from string instead of your list - and probably want to use some other functions to draw from the list:

import string
import random

# prints random 20 lowercase chars w/o repeat
print(random.sample(string.ascii_lowercase,20))    # see doku for sample()

# print random 20 lowercase chars with repeat
print(random.choices(string.ascii_lowercase,k=20)) # see doku for choices()

# prints the same char multiple times
print(random.choice(string.ascii_lowercase) * random.randint(5,20))

Output:

['i', 'q', 's', 'z', 'g', 'v', 'r', 'j', 'h', 'u', 'y', 'p'
 , 'n', 't', 'k', 'c', 'm', 'a', 'x', 'd']

['f', 'x', 'u', 'x', 'a', 'l', 'f', 'u', 'l', 'x', 'j', 'i'
 , 'v', 'f', 'd', 'u', 'l', 'x', 'j', 'w']

rrrrrrrr  # this one varies in char and amounts...

Doku of random - functions read up about sample, choices and random

Doku of string - constants your lower case list is already a constant there.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.