我正在尝试创建一个随机字符串生成器,它可以生成长度为min <= n <= max的字符串。但是,我仍然对编码字符感到困惑。我让它们显示在打印行上,但它没有显示为正确的长度。我有没有做过正确的事情?
我的意见:aäæeiïuḅḅv v g g g y j j j j j j x 五 10
from random import *
#Class random generator
#@variables list aList,boolean again, int maximum, int minimum
class randomStringGenerator:
def __init__(this):
aList = []
allowed = input("What are the characters that you will allow?\n" +
"Please press the chracters you want and leave a space between each character\n, then enter to submit\n" +
"e.g. 'a' 'b' 'c'\n ")
aList = allowed.split(" ")
minimum = input("What is the minimum number of characters in the string that you want? \n")
maximum = input("What is the maximum number of characters in the string that you want? \n")
this.aList = aList
this.minimum = int(minimum)
this.maximum = int(maximum)
again = True
this.again = again
#generateRandNum generates a random int from a minimum to b maximum
#@param this,mini,maxi
#@return int x the integer that was chosen
def generateRandNum(this,mini,maxi):
x = randint(mini,maxi)
return x
#generateRandString generates the actual string randomly
def generateRandString(this):
ans = ""
strSize = this.generateRandNum(this.minimum,this.maximum)
pos = 0
while(pos < strSize):
size = len(this.aList)
idx = this.generateRandNum(0,size - 1)
char = this.aList[idx]
ans = ans + char
pos += 1
ans.encode('utf-8')
print(ans)
def getAgain(this):
return this.again
def replay(this):
x = input("Would you like to generate another string using the same settings? y/n \n")
if(x == "y"):
this.generateRandString()
else:
y = input("Would you like to generate another string using different settings? y/n \n")
if(y == "y"):
new = input("What are the new characters that you will allow? ")
mini = input("What is the new minimum number of characters? ")
maxi = input("What is the new maximum number of characters? ")
this.aList = new.split(" ")
this.minimum = int(mini)
this.maximum = int(maxi)
this.generateRandString()
else:
this.again = False
s = randomStringGenerator()
s.generateRandString()
while(s.getAgain()):
s.replay()
示例输出:正如您所看到的,我将最小字符设置为5,但只显示2个,并且出于某种原因多次打印......
What are the characters that you will allow?
Please press the chracters you want and leave a space between each character
, then enter to submit
e.g. 'a' 'b' 'c'
a ä æ e i ï o u ü b ḅ d ḍ f v h ḥ k g l ľ m y n ṇ p ṕ r ṛ s j w ẉ x x̣
What is the minimum number of characters in the string that you want?
5
What is the maximum number of characters in the string that you want?
10
o
oḅ
oḅ
oḅ
oḅ
oḅ
oḅ
答案 0 :(得分:1)
您的代码中存在多个问题。
self
而不是this
。if __name__ == "__main__"
encoding: utf-8
指定编码。_prepare()
更新的代码如下:
#encoding:utf-8
from random import *
class randomStringGenerator:
def __init__(self):
self.aList = []
self.again = True
self._prepare()
def _prepare(self):
allowed = input("What are the characters that you will allow?\n"
"Please press the chracters you want and leave a space between each character\n, then enter to submit\n"
"e.g. 'a' 'b' 'c'\n")
aList = allowed.split(" ")
minimum = input("What is the minimum number of characters in the string that you want? \n")
maximum = input("What is the maximum number of characters in the string that you want? \n")
self.aList = aList
self.minimum = int(minimum)
self.maximum = int(maximum)
def generateRandNum(self, mini, maxi):
x = randint(mini,maxi)
return x
def generateRandString(self):
ans = u""
strSize = self.generateRandNum(self.minimum, self.maximum)
pos = 0
while(pos < strSize):
size = len(self.aList)
idx = self.generateRandNum(0, size - 1)
char = self.aList[idx]
ans = ans + char
pos += 1
ans.encode('utf-8')
return ans
def getAgain(self):
return self.again
def replay(self):
x = input("Would you like to generate another string using the same settings? y/n \n")
if(x == "y"):
return self.generateRandString()
else:
y = input("Would you like to generate another string using different settings? y/n \n")
if(y == "y"):
self._prepare()
return self.generateRandString()
else:
self.again = False
return ""
if __name__ == "__main__":
s = randomStringGenerator()
print (s.generateRandString())
while(s.getAgain()):
print (s.replay())
输出为:
python3 str_1.py
What are the characters that you will allow?
Please press the chracters you want and leave a space between each character
, then enter to submit
e.g. 'a' 'b' 'c'
a ä æ e i ï o u ü b ḅ d ḍ f v h ḥ k g l ľ m y n ṇ p ṕ r ṛ s j w ẉ x x̣
What is the minimum number of characters in the string that you want?
5
What is the maximum number of characters in the string that you want?
10
bpjwdsndv
Would you like to generate another string using the same settings? y/n
y
radsṛreyrx
Would you like to generate another string using the same settings? y/n
n
Would you like to generate another string using different settings? y/n
y
What are the characters that you will allow?
Please press the chracters you want and leave a space between each character
, then enter to submit
e.g. 'a' 'b' 'c'
a ä æ e i ï o u ü b ḅ d ḍ f v h ḥ k g l ľ m y n ṇ p ṕ r ṛ s j w ẉ x x̣
What is the minimum number of characters in the string that you want?
5
What is the maximum number of characters in the string that you want?
8
dnṛnhṛv
Would you like to generate another string using the same settings? y/n
n
Would you like to generate another string using different settings? y/n
n