2

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.

My Working Version

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
Community
  • 1
  • 1
M. Husnain
  • 45
  • 1
  • 1
  • 3
  • There are many problems in your code... no need to use `str` around `input()`, your name variables are being deleted right after the input by doing `name1=a` etc... you are not using `random.randint` right at all.... I suggest you attempt smaller pieces to see if they even work before joining them all to a program. If you have specific questions free free to open more detailed question about the actual errors, but with *minimal* examples – Ofer Sadan Jul 31 '17 at 11:41

6 Answers6

7
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'
Kallz
  • 3,244
  • 1
  • 20
  • 38
1

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

Coding thermodynamist
  • 1,340
  • 1
  • 10
  • 18
1

Hello M. Husnain,

Chr() Function

chr(i)
Return the string representing a character whose Unicode code point is the integer i. For example, chr(97) returns the string 'a', while chr(8364) returns the string '€'. This is the inverse of ord().

The valid range for the argument is from 0 through 1,114,111 (0x10FFFF in base 16). ValueError will be raised if i is outside that range.

Solution

Try this below code,

#This program will play a little game

import random

a = ''
b = ''
c = ''
d = ''
e = ''
f = ''

print('Hi. Please enter your name in letters')
name = raw_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 = raw_input()

print('Please enter the second name in letters')
name2 = raw_input()

print('Please enter the third name in letters')
name3 = raw_input()

print('Please enter the fourth name in letters')
name4 = raw_input()

print('Please enter the fifth name in letters')
name5 = raw_input()

print('Please enter the sixth name in letters')
name6 = raw_input()

print('Alright ' + name + ' . Thank you for entering the names.')

lists1 = [name1,name2,name3,name4,name5,name6]

secretname = lists1[random.randint(0,len(lists1))]

for i in range(2):
        print('Now enter the name that i am thinking of.')
        ans = raw_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)

I hope my answer is helpful.
If any query so comment please.

Mayur Vora
  • 922
  • 2
  • 14
  • 25
0

try this,

import string
alpha = string.ascii_uppercase
num = string.digits
''.join(random.choice(alpha + num) for _ in range(5)) #number you want
Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
0

Instead of saving the names into different variables I'd propose a list containing the names entered. It could look something like this:

name_list = []
print('Please enter the first name in letters')
name_list.append(input())
print('Please enter the second name in letters')
name_list.append(input())
...

You can then randomly select a list item using random.choice e.g.

import random
secretname = random.choice(name_list)
Fynn Becker
  • 1,278
  • 2
  • 18
  • 21
0

Update:

For the specific case you described, you can simply create a list of characters containing all of the characters you allow. then you can use this, to create a word of length X (replace with actual length:

base_chars = [ 'a', 'b', 'c', '1', '2', '3' ]
word_length = 10 #change this to the desired word length
[random.choice(base_chars) for _ in range(word_length)]

Original:

Random string - define your characters

You should first decide what kind of characters you want to use. For example, if you want to use ASCII chars from 0 to 127 or maybe just a-z and A-Z. Also consider digits, space etc. So you should first decide the set of characters you want to select randomly from.

Selecting a character randomly

If you would like to use 0 to 127 range in the ASCII table, you can use this:

char = chr(random.randint(0, 127))

Generating a random word

Now, to create a word, you should first decide the size of it. This can also be randomize. For example, we will randomly choose a size of a word (with limited range):

rand_str = ""
for _ in range(10):
     rand_str += chr(random.randint(0, 127))
Ori
  • 1,680
  • 21
  • 24