简体   繁体   English

将变量放在python中的引号内

[英]putting a variable inside of quotes in python

I have just started learning Python. 我刚刚开始学习Python。 I am trying to have the user input a number base, and a number, and convert that to decimal. 我试图让用户输入一个数字基数和一个数字,并将其转换为十进制。

I've been trying to use the built in int function like this: 我一直在尝试使用内置的int函数,如下所示:

base = raw_input("what number base are you starting with?  \n")
num = raw_input("please enter your number:  ")
int(num,base)

My problem is that when you use that int function, the number your converting needs to be in quotes like so: int('fff',16) 我的问题是,当您使用该int函数时,转换的数字必须用引号引起来,例如: int('fff',16)

How can I accomplish this when im using a variable? 即时通讯使用变量时,该如何完成?

Quotes are not needed. 不需要引号。 The quotes are not part of the string. 引号不是字符串的一部分。

>>> int('fff',16)
4095
>>> da_number = 'fff'
>>> int(da_number, 16)
4095

You will, however, need to cast the base to an integer. 但是,您需要将基数转换为整数。

>>> base = '16'
>>> int(da_number, base) # wrong! base should be an int.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required
>>> int(da_number, int(base)) # correct
4095

The result of raw_input() will be a string, I think your issue is actually that base is still a string when you need it to be an integer. raw_input()的结果将是一个字符串,我认为您的问题实际上是当您需要将base作为整数时, base仍然是字符串。

Try the following: 请尝试以下操作:

base = raw_input('base? ')
num = raw_input('num? ')
print int(num, int(base))

For example: 例如:

>>> base = raw_input('base? ')
base? 16
>>> num = raw_input('num? ')
num? fff
>>> print int(num, int(base))
4095

The problem isn't quotes. 问题不在于引号。 The problem is with data types . 问题出在数据类型上 int with a base expects an integer base and a string number . 带有基数的int期望整数基数字符串数 So, in your case, you'd use: 因此,就您而言,您将使用:

int(num,int(base))

This takes the strings which are returned from raw_input and it constructs a suitable base from the base string while leaving num as a string as required by the int function. 这将使用从raw_input返回的字符串 ,并根据base字符串构造一个合适的基础,同时将num作为int函数所需的字符串保留。

The quotes are there to tell Python that those characters should be interpreted as the contents of a String, not as language syntax. 用引号告诉Python这些字符应解释为String的内容,而不是语言语法。

When you have a variable, Python already knows that the contents are a string. 当您拥有一个变量时,Python已经知道内容是一个字符串。 Not only do you not need the quotes, but adding them would confuse Python into thinking that you wanted a string holding the name of the variable. 您不仅不需要引号,而且添加引号还会使Python误以为您想要一个包含变量名的字符串。

So, both this: 因此,这两个:

int('fff',16)

and this: 和这个:

someNumber = 'fff'
int(someNumber,16)

are doing the same thing as far as the int() function is concerned. int()函数而言,它们正在做相同的事情。

All that being said, int() can take a string or a number for the value, but must have a number for the base. 话虽如此, int()可以使用字符串或数字作为值,但必须有一个数字作为基数。 So, since raw_input always returns a string, you'd need to do something more like: 因此,由于raw_input始终返回一个字符串,因此您需要执行以下操作:

base = raw_input("what number base are you starting with?  \n")
num = raw_input("please enter your number:  ")
int(num,int(base))

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

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