在Python 3中将%r与input()一起使用 [英] Using %r with input() in python 3

查看:97
本文介绍了在Python 3中将%r与input()一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,遇到了一些麻烦.如果你们能帮助解释如何做到这一点,以及它为什么以这种方式起作用,那就太好了.我在python 3的第11课中尝试学习python,试图进行更改.我已经找了最后一个小时左右,试图弄清楚这一点.我已经尝试过各种(),","组合.谢谢您的帮助.我正在努力从我完成的codecademy python教程过渡到python 3.这是我最初写的,因为我被它弄乱了.

I'm new to python and having a bit of trouble. If you guys could help explain how to do this and why it works the way it does would be awesome. I'm doing the learn python the hard way lesson 11 in python 3 trying to make the change over. I've looked for the last hour or so trying to figure this out. I have tried all kinds of (), " ", ' ' combo's. Thanks for the help. I'm struggling with the step over to python 3 from codecademy python tutorials I've done. Here is what I originally wrote be for i messed with it.

我尝试将其放在()之外并放在自己的()中,但我一直在读取错误.

I have tried putting it outside the () and in there own () but I keep reading errors.

print ("How old are you?"),
age = input("Enter: ")
print ("How tall are you?"),
height = input("Enter:")
print ("What are you in to?"),
interest = input("Enter: ")

print ("So your %r and %r tall. Cool what do you like?"),
print ("I like  to do %r "),

此代码运行正常,但%r未被用户input()取代.

This code is running fine but the %r is not replacing with the user input().

推荐答案

您需要提及要替换的变量:

You need to mention which variables you want to substitute:

print("So your %r and %r tall. Cool what do you like?" % (age, height))

字符串%r在任何方面都不是特殊的:它只是格式运算符%可以识别的占位符值.

The string %r is not special in any way: it is just a placeholder value that is recognized by the formatting operator %.

注意:尾部逗号在Python 3中无法完成任何操作,因为print只是一个常规函数.如果要取消显示换行符,请改用以下内容:

Note: the trailing comma does not accomplish anything in Python 3 because print is just a regular function. If you want to suppress the newline, use the following instead:

print(..., end="")

通常,在阅读Python文档,指南和教程时,最好检查它针对的 version . Python 2和3有一些明显的区别.

In general, when reading Python documentation, guides, and tutorials, it's a good idea to check what version it targets. Python 2 and 3 have some significant differences.

这篇关于在Python 3中将%r与input()一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆