简体   繁体   English

如何在python中将变量更改为字符串?

[英]How do You change a variable to a string in python?

So I am trying to change a randomized variable to a string with a function, any ideas why this isn't working? 因此,我正在尝试将随机变量更改为带有函数的字符串,为什么不起作用?

def letter(x):
    if x == 1:
            x = "A"
    elif x == 2:
            x = "C"
    elif x == 3:
            x = "G"
    elif x == 4:
            x = "T"
    else:
            print "Error" 
randint18= random.randrange(1,5)
letter(randint18)
print randint18 `

You have to return the value from the function, and assign it to a variable. 您必须从函数返回值,并将其分配给变量。

def letter(x):
    ...
    return x


randint18 = random.randrange(1, 5)
result = letter(randint18)
print result

You cannot alter the variable in place you must return it and capture the returned value. 您不能就地更改变量,必须将其返回并捕获返回的值。

import random

def letter(x):
    if x == 1:
            x = "A"
    elif x == 2:
            x = "C"
    elif x == 3:
            x = "G"
    elif x == 4:
            x = "T"
    else:
        print "Error" 
    return x  # return it here

randint18= random.randrange(1,5)
randint18 = letter(randint18)  # capture the returned value here 
print randint18

There is a simpler way to achieve what you want, using a dictionary to map the values. 使用字典映射值,有一种更简单的方法来实现所需的目标。

import random

def letter(x):
    mapd = {1:'A', 2:'C', 3:'G', 4:'T'}
    return mapd.get(x, None)

randint18= random.randrange(1,5)
randint18 = letter(randint18)
print randint18

mine isn't a proper answer, which have been provided already, but a suggestion for improving your code. 我的不是一个正确的答案,已经提供了它,但是它是改善代码的建议。 I'd do it in a comment, but the code formatting ain't good enough. 我会在评论中这样做,但是代码格式不够好。

Why not use a dictionary for the mapping, instead of a sequence of if's? 为什么不使用字典而不是if序列呢? You could still place it in a function if you like: 如果愿意,仍可以将其放置在函数中:

letter = {1:'A', 2:'C', 3:'G', 4:'T'}
randint18 = random.randrange(1,5)
mapping = letter.get(randint18, 'Error')
print mapping

mind you, a list would be even more efficient, if the mapping started form zero: 请注意,如果映射从零开始,列表将更加有效:

letter = ['A', 'C', 'G', 'T']
randint18 = random.randrange(0,4)
try: # in case your random index were allowed to go past 3
 mapping = letter[randint18]
except IndexError:
 mapping = 'Error'
print mapping

You forgot to include a return in your function 您忘记在函数中包含返回

def letter(x):
    if x == 1:
            x = "A"
    elif x == 2:
            x = "C"
    elif x == 3:
            x = "G"
    elif x == 4:
            x = "T"
    else:
            print "Error" 
    return x
randint18 = random.randrange(1,5)
returned_result = letter(randint18)
print returned_result

Add a return value of the function 添加函数的返回值

return x

value_you_want = letter(randint18) ##add the return statement. value_you_want = letter(randint18)##添加返回语句。 Output will be saved to value_you_want 输出将保存到value_you_want

Please note that the variables defined inside a function are local to the function and cannot be accessed outside the scope of the function. 请注意,在函数内部定义的变量是函数的局部变量,不能在函数范围之外访问。 You were expecting the value of x outside the function which is not possible. 您期望函数之外的x值是不可能的。 Just to check run your function and try to access the value in variable x. 只是检查运行您的函数并尝试访问变量x中的值。 It will give error. 它将给出错误。

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    print x
NameError: name 'x' is not defined

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM