【问题标题】:Replacing a random character of string with another character用另一个字符替换字符串的随机字符
【发布时间】:2021-06-12 01:21:34
【问题描述】:

关于上下文:我正在制作一个简单的 Python 游戏,您可以在其中四处走动并收集浆果。 如果你想测试它,我的整个代码看起来像这样。

import keyboard
import time
import emoji
import random

def assignstring():
    print("~~~~~~~~~~~~~~~~")
    s = ""
    bers1 = random.randint(0, 22)
    bers2 = random.randint(0, 10)
    print(bers1, bers2)
    for a in range(9):
        for i in range(21):
            if i == posx and a == posy:
                s = s + emoji.emojize(":bear:")
            elif a == bers1 and i == bers2:
                s = s + "!"
            else:
                s = s + emoji.emojize(":prohibited:")
        print(s)
        s = ""
    print("~~~~~~~~~~~~~~~~")

def poschange(posx, posy):
    if keyboard.is_pressed("left"):
        posx = posx - 1
        time.sleep(0.1)
    if keyboard.is_pressed("right"):
        posx = posx + 1
        time.sleep(0.1)
    if keyboard.is_pressed("down"):
        posy = posy + 1
        time.sleep(0.1)
    if keyboard.is_pressed("up"):
        posy = posy - 1
        time.sleep(0.1)
    if posx > 20:
        posx = 20
    if posx < 0:
        posx = 0
    if posy < 0:
        posy = 0
    if posy > 8:
        posy = 8
    return posx, posy

string = ""
posx = 10
posy = 4
c = 11
savex = 0
savy = 0
assignstring()


while True:
    savex = posx
    savey = posy
    posx = (poschange(posx, posy))[0]
    posy = (poschange(posx, posy))[1]
    if savex != posx:
        assignstring()
        print(posx, posy)
    if savey != posy:
        assignstring()
        print(posx, posy)

我想制作一个函数,用浆果表情符号替换一个被禁止的表情符号。 这是我写的一些伪代码:

def berryspawn():
    berrycord1 = random.randint(0, 10)
    berrycord1 = random.randint(0, 22)
    #saves the coordinates and replaces the emoji at that location with a berry emoji.

我希望它保存坐标,以便它可以跟踪玩家是否正在触摸浆果。 最简单的方法是什么?

问候迈克

【问题讨论】:

  • 你不能把它存储为一个全局变量吗?
  • 我想是坐标部分,但是替换替换部分呢......并且:返回坐标会不会更容易
  • 是的,这也是一个很好的解决方案!您可以在函数内更改全局变量 btw(请参阅 w3schools.com/python/python_variables_global.asp

标签: python string function replace coordinates


【解决方案1】:

我想我可以帮助您更改字符串部分中的随机字符。

这是我要做的,一步一步完成(尝试和测试):

import random # need to use randints

mystring = "foo" # this'd be your string


# Now to get the string length so we can get a random char from it. First to 
# turn it to a list, as individual characters in strings can't be changed.

mystring_list = list(mystring)

mystring_list[random.randint(0, (len(mystring_list) - 1))] = # add your new 
# character value here

# Finally you need to change the list back to its string form. We'll also 
# remove all []s and 's plus spaces and commas so it's back to complete string 
# format.

mystring = str(mystring_list).replace('[', "").replace(']', "").replace("'", 
"").replace(",", "").replace(" ", "")

我认为这可以解决问题。流程分解:

  1. mystring 变成了一个包含所有字符的列表,mystring_list
  2. 在 Random 模块的帮助下,我们在 mystring_list 中获得了一个随机项
  3. 然后那个项目被改变了。
  4. mystring 然后设置为 mystring_list 的字符串,其中括号/逗号/空格/撇号/引号已删除。所以基本上是一个字符串。

唯一的问题是,如果您在原始字符串中有空格,那么它们将在最后被删除(第 4 步)。如果您不介意每个字符后原始字符串中的奇数空格,则可以删除.replace(" ", ""),原始空格将被保留。

希望我能有所帮助!

【讨论】:

  • 参见stackoverflow.com/questions/1228299/…了解更多关于修改字符串的信息
  • 这真的很接近我要找的东西,但是有两个问题 1. 输出看起来像这样:???????????????? ?????\n?????????????????????\n???????????????????? ?\n?????????????????????\n?????????????????!???\n? ????????????????????\n?????????????????????\n????? ????????????????\n‖2.它需要保存哪个角色的坐标它取代了
  • 1) 要获取已更改的字符,请将变量(称为 changed_int)设置为 random.randint(0, (len(mystring_list) - 1))。 2)然后,当您更改列表中的字符时,使用[changed_int] 而不是[random.randint(0, (len(mystring_list) - 1))](否则它将获得一个新号码;您想要相同的号码)。希望这会有所帮助。
  • 对于第一个问题,我真的不知道发生了什么。也许,你能告诉我原始字符串是什么样的吗?它是否包含表情符号?
  • 更新:我修复了 \n 字符串问题。
猜你喜欢
  • 1970-01-01
  • 2019-05-14
  • 1970-01-01
  • 2017-05-20
  • 2021-06-15
  • 1970-01-01
  • 2013-12-03
  • 2017-08-06
  • 2017-04-01
相关资源
最近更新 更多