如何用python随机生成字符串_如何生成随机字符串?

我只想问一下,就像从:Random.randint(a, b)中取随机数一样

我只想问一下如何使用随机字符串,就像randint但是这次是使用随机字符串。还有吗?#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)

这是一个小游戏,要求你输入6个名字,然后游戏会猜测你输入的6个数字之间的一个数字,但它总是给我一个错误。想用随机字符串来做。

我的工作版本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)

输出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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值