I just want to ask that like taking random number from: Random.randint(a, b)
I just want to ask that how to take random string just like randint but this time with random string. Is there anyway?
#This program will play a little game
import random
a = ''
b = ''
c = ''
d = ''
e = ''
f = ''
print('Hi. Please enter your name in letters')
name = str(input())
print('Hi ' + name + '. I am going to play a little game. In this game you have to guess a specific name i am thinking right now.')
print('But do not worry. I am going to let you to enter 6 names and i will choose one of them.')
print('After that you have to answer the correct name that i am thinking right now')
print('Please enter the first name in letters')
name1 = str(input())
print('Please enter the second name in letters')
name2 = str(input())
print('Please enter the third name in letters')
name3 = str(input())
print('Please enter the fourth name in letters')
name4 = str(input())
print('Please enter the fifth name in letters')
name5 = str(input())
print('Please enter the sixth name in letters')
name6 = str(input())
name1 = a
name2 = b
name3 = c
name4 = d
name5 = e
name6 = f
print('Alright ' + name + ' . Thank you for entering the names.')
secretname = random.randint(a, f)
for i in range(2):
print('Now enter the name that i am thinking of.')
ans = str(input())
if ans != secretname:
print('Wrong. Guess another name')
if ans == secretname:
print('Good job ' + name)
else:
print('Wrong. The name i was thinking of was ' + secretname)
This is a little game which request from you to enter 6 names and then the game will guess a number between those 6 numbers you have entered but it always gaves me an error. want to do it with random string.
import random
print('Hi. Please enter your name in letters')
yourname = str(input())
print('Hi ' + yourname + '. I am going to play a little game. In this game you have to guess a specific name i am thinking right now.\nBut do not worry. I am going to let you to enter 6 names and i will choose one of them.\nAfter that you have to answer the correct name that i am thinking right now\nPlease enter the first name in letters')
name = ["", "", "", "", "", ""]
number = ["first", "second", "third", "fourth", "fifth", "sixth"]
def insert(n):
temp = name.copy()
name[n] = str(input("name " + str(n + 1) + ": "))
if name[n] in temp:
print("[error]: ---> This name exists yet, try again")
print(">>>", end='')
insert(n)
for n in range(6):
insert(n)
print('Please enter the ' + number[n] + ' name in letters')
print('Alright ' + yourname + ' . Thank you for entering the names.')
secretname = name[random.randint(0, 6)]
print("Soluzion: ", secretname)
print("-" * 10)
for i in range(2):
print('Now enter the name that i am thinking of.')
ans = str(input())
if ans != secretname:
print('Wrong. Guess another name')
if ans == secretname:
print('Good job ' + yourname)
else:
print('Wrong. The name i was thinking of was ' + secretname)
Output
Hi. Please enter your name in letters Gio Hi Gio. I am going to play a little game. In this game you have to guess a specific name i am thinking right now. But do not worry. I am going to let you to enter 6 names and i will choose one of them. After that you have to answer the correct name that i am thinking right now Please enter the first name in letters name 0: Giovanni Please enter the first name in letters name 1: Marta Please enter the second name in letters name 2: Giovanni [error]: ---> This name exists yet, try again >>>name 2: Giovanni [error]: ---> This name exists yet, try again >>>name 2: Carlo Please enter the third name in letters name 3: Mimmo Please enter the fourth name in letters name 4: June Please enter the fifth name in letters name 5: Caterina Please enter the sixth name in letters Alright Gio . Thank you for entering the names. Solution: Mimmo ---------- Now enter the name that i am thinking of. M Wrong. Guess another name Now enter the name that i am thinking of. Mimmo Good job Gio
import string
import random
def random_string(length):
return ''.join(random.choice(string.ascii_letters) for m in xrange(length))
print random_string(10)
print random_string(5)
Output:
'oJyPiEbhka'
'XXwuA'
You can do it using random.randint :
my_string = "abcdefghijklmnopqrstuvwxyz"
index = random.randint(0,25)
letter = my_string[index]
You just have to loop over that to build strings from letters
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With