Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create a word
#1
This is my oldest game that is saved. I really like it so please try it out Heart !

Thanks, foksikrasa

import random

alphabet = ["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"]
print("how much letters should the word have, from 0 to 25?")
print()
a = int(input())
str = ""
for i in range(a):
  str = str + alphabet[random.randint(0,25)]
print("the word is ", str)
Reply
#2
don't use str as variable name. It's a built-in function and you override it
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Ok, thanks for the suggestion.
Reply
#4
Made a little modification
import random

def main():
    alphabet = ["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"]
    answers = int(input("How much letters should the word have, (From 0 to 25): "))
    value_store = ''
    for i in range(answers):
        value_store = value_store + alphabet[random.randint(0, 25)]
    print('The word is: ', value_store)

if __name__ == '__main__':
    main()
Reply
#5
A lot of modification
import random
import string

amount = int(input('How many letters should the word have?'))
word = ''.join((random.choice(string.ascii_lowercase) for _ in range(amount)))
print(f'The word is {word}')
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020