python中关于输出语句中加r

python基础之输出忽略转译符

在我们网上看别人代码时,有时可能会出现这样的语句

print(r'*******')

那么语句中的r是干什么用的呢?
你输入一个语句,如果你输入以下

print('hello word')
print(r'hello word')

你会发现两者的输出结果都是一样的
hello word
而如果我们输入两种特殊的语句

print('mmmmm\n123')

我们知道\n是换行符的意思,所以上述结果输出即是
mmmmm
123
而有时我们可能需要\n这样的字符串,因此,我们就要采用输出字符串前加r的方法

print(r'mmmmm\n123')

输出就会发现
mmmmm\n123
有我们想要的\n,所以r的意思就是告诉机器,我们要用到语句中的特殊符号。

发布了17 篇原创文章 · 获赞 6 · 访问量 1万+
展开阅读全文

Python if语句输出

06-21

下面是我的代码,主要在于points函数,它不执行if里面的内容,但也没有报错 ``` # -*- coding:utf-8 -*- import random import time import sys playerPoints = 100 playerWon = False playerChoice = None playerGuess = None def easy(): ## 简单难度的踩雷,在 1 ~ 100 之间随机选择雷号 global playerWon rbnList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36, 37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69, 70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100] rbn = random.sample(rbnList,3) if playerGuess == rbn[0] or playerGuess == rbn[1] or playerGuess == rbn[2]: sys.stdout.write("你踩到地雷了!") playerWon = False points() else: sys.stdout.write("你躲开了地雷!") playerWon = True points() def normal(): ## 正常难度的踩雷,在 1 ~ 50 之间随机选择雷号 global playerWon rbnList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36, 37,38,39,40,41,42,43,44,45,46,47,48,49,50] rbn = random.sample(rbnList,3) if playerGuess == rbn[0] or playerGuess == rbn[1] or playerGuess == rbn[2]: sys.stdout.write("你踩到地雷了!") playerWon = False else: sys.stdout.write("你躲开了地雷!") playerWon = True points() def hard(): ## 困难难度的踩雷,在 1 ~ 20 之间随机选择雷号 global playerWon rbnList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20] rbn = random.sample(rbnList, 3) if playerGuess == rbn[0] or playerGuess == rbn[1] or playerGuess == rbn[2]: sys.stdout.write("你踩到地雷了!") playerWon = False else: sys.stdout.write("你躲开了地雷!") playerWon = True points() def points(): ## 玩家分数,踩雷游戏分数系统 global playerPoints if playerWon == True: if playerChoice == "简单难度": playerPoints += 5 sys.stdout.write("当前分数加5, 当前分数: %s" % playerPoints) elif playerChoice == "正常难度": playerPoints += 10 sys.stdout.write("当前分数加10, 当前分数: %s" % playerPoints) elif playerChoice == "困难难度": playerPoints += 20 sys.stdout.write("当前分数加20, 当前分数: %s" % playerPoints) elif playerWon == False: if playerChoice == "简单难度": playerPoints -= 5 sys.stdout.write("当前分数减5, 当前分数: %s" % playerPoints) if playerPoints < 0: sys.stdout.write("分数过少,不能进行减分,请等待分数重置") playerPoints = 0 resetPoints() elif playerChoice == "正常难度": playerPoints -= 7 sys.stdout.write("当前分数减7, 当前分数: %s" % playerPoints) if playerPoints < 0: sys.stdout.write("分数过少,不能进行减分,请等待分数重置") playerPoints = 0 resetPoints() elif playerChoice == "困难难度": playerPoints -= 10 sys.stdout.write("当前分数减10, 当前分数: %s" % playerPoints) if playerPoints < 0: sys.stdout.write("分数过少,不能进行减分,请等待分数重置") playerPoints = 0 resetPoints() def resetPoints(): resetPointsTimes = 300 for i in range(resetPointsTimes): sys.stdout.write("分数将在 {} 秒后重置为100".format(resetPointsTimes - i)) sys.stdout.flush() time.sleep(1) sys.stdout.write("分数成功重置为100") GameBody() def rules(): sys.stdout.write("==================== *游戏规则* ====================") sys.stdout.write("本游戏分3个模式:简单模式,正常模式和困难模式") sys.stdout.write("简单模式: 赢了得5分, 输了扣5分") sys.stdout.write("正常模式: 赢了得10分, 输了扣7分") sys.stdout.write("困难模式: 赢了得20分, 输了扣10分") sys.stdout.write("祝您游戏愉快!") def GameBody(): global playerChoice global playerGuess playerChoice = input("简单模式请输入'简单模式',正常模式请输入'正常模式'," + "困难模式请输入'困难模式',查看游戏规则请输入'规则':") if playerChoice == "简单模式": playerGuess = int(input("请输入一个 1 ~ 100 的数字")) easy() elif playerChoice == "正常模式": playerGuess = int(input("请输入一个 1 ~ 50 的数字")) normal() elif playerChoice == "困难模式": playerGuess = int(input("请输入一个 1 ~ 20 的数字")) hard() elif playerChoice == "规则": rules() else: sys.stdout.write("输入错误, 请重新输入") GameBody() GameBody() ``` 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览