【发布时间】:2018-01-06 23:06:20
【问题描述】:
我只是想问一下,喜欢从: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
【问题讨论】:
-
你的代码有很多问题......不需要在
input()周围使用str,你的名字变量在输入后通过name1=a等被删除......你根本没有使用random.randint....我建议您在将它们全部加入程序之前尝试更小的部分,看看它们是否有效。如果您有具体问题,请免费打开有关实际错误的更详细问题,但只有 minimal 示例