鱼C论坛

 找回密码
 立即注册
查看: 1729|回复: 12

[已解决]python 通过文字游戏的学习后敲得一段代码 求助求助有点小问题

[复制链接]
发表于 2019-2-19 18:46:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 IamBayMax 于 2019-2-19 19:08 编辑

print("welcome BayMax Secret")
temp=input("Please input account number:")
guess = int(temp)
if guess == 1637000313:
    print("Correct account number:1637000313")
    temp2=input("Please enter your password:")
    guess = int(temp2)
    if guess == 110110110110:
        print ("welcome")
    else:
        print ("Sorry, the password is wrong. Call the 小甲鱼 to get the universal password")
else:
    print("..who are you..Your invasion has informed the owner")



目前能完美运行  如果我想要在输错密码后能重新再输入应该做什么调整呢

开心 做了一个有趣的东西
   
最佳答案
2019-2-20 22:31:41
IamBayMax 发表于 2019-2-20 00:55
老师您写的的代码走的程序还是原来那样嘛
guess=0和while guess !是什么意思呢

guess=0和while guess !是你原来的呀,我没有改;
你说的还是原来那样是不是指第一种,第一种确实不会提醒用户“Please give an integer!”,因为第一种只是把密码检测由数字改为字符串,用户输入非整数就不会报错,后面两种就可以满足你的需求了吧。
刚刚看了一下发现第三种有一个小错误,第15行guess = int(temp)应该改为guess = int(temp2),不好意思。
另外,你的代码思路应该是先让用户循环输入正确的用户名,然后再循环输入正确的密码,对吧?但你原来的写法(包括我昨晚的三段代码,没有改你的思路),如果用户在输入用户名阶段输入了110110110110,就会跳出循环。我觉得要实现你的想法,像下面这样可能会更好,不必在循环里套循环,直接分成两段,更明晰。
while True:
    print("welcome BayMax Secret")
    temp=input("Please input account number:")
    try:
        guess = int(temp)
    except ValueError:
        print('Please give an integer!')
        continue
    if guess == 1637000313:
        print("Correct account number:1637000313")
        break
    else:
        print("..who are you..Your invasion has informed the owner")

while True:
    temp2=input("Please enter your password:")
    try:
        guess = int(temp2)
    except ValueError:
        print('Please give an integer!')
        continue
    if guess == 110110110110:
        print ("welcome")
        break
    else:
        print ("Sorry, the password is wrong. Call 小甲鱼 to get the universal password")

By the way, 我不是老师,也是小白一枚~~
   

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-2-19 19:09:29 | 显示全部楼层
While Ture:循环
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-2-19 20:57:32 | 显示全部楼层
在开头加上'while True:'后面的代码缩进
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-2-19 21:55:07 | 显示全部楼层
本帖最后由 杯具菜鸟 于 2019-2-19 21:56 编辑

guess = 0
while guess != 110110110110:
    print("welcome BayMax Secret")
    temp=input("Please input account number:")
    guess = int(temp)
    if guess == 1637000313:
        print("Correct account number:1637000313")
        while guess != 110110110110:
            temp2=input("Please enter your password:")
            guess = int(temp2)
            if guess == 110110110110:
                print ("welcome")
            else:
                print ("Sorry, the password is wrong. Call the 小甲鱼 to get the universal password")
    else:
        print("..who are you..Your invasion has informed the owner")

我是初学者。。。。瞎改的。。。
现在有个问题是,如果输入的不是整数,程序就会崩溃

顺便问一下,有什么办法判断,如果输入的不是整数,就要求重新输入,痛苦学习中
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-2-19 22:19:53 | 显示全部楼层
杯具菜鸟 发表于 2019-2-19 21:55
guess = 0
while guess != 110110110110:
    print("welcome BayMax Secret")

学习用try except语句就好了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-2-19 22:44:28 | 显示全部楼层
杯具菜鸟 发表于 2019-2-19 21:55
guess = 0
while guess != 110110110110:
    print("welcome BayMax Secret")

我能想到三种方法
第一种,对于你来说应该最容易理解,直接把密码检测改成字符串,就不用int()转数字了,具体如下:
  1. guess = 0
  2. while guess != '110110110110':
  3.     print("welcome BayMax Secret")
  4.     guess=input("Please input account number:")
  5.     if guess == '1637000313':
  6.         print("Correct account number:1637000313")
  7.         while guess != '110110110110':
  8.             guess=input("Please enter your password:")
  9.             if guess == '110110110110':
  10.                 print ("welcome")
  11.             else:
  12.                 print ("Sorry, the password is wrong. Call the 小甲鱼 to get the universal password")
  13.     else:
  14.         print("..who are you..Your invasion has informed the owner")
复制代码


第二种,用字符串方法判断输入是否为数字,具体如下:
  1. guess = 0
  2. while guess != 110110110110:
  3.     print("welcome BayMax Secret")
  4.     temp=input("Please input account number:")
  5.     if temp.isdigit():
  6.         guess = int(temp)
  7.     else:
  8.         print('Please give an integer!')
  9.         continue
  10.     if guess == 1637000313:
  11.         print("Correct account number:1637000313")
  12.         while guess != 110110110110:
  13.             temp2=input("Please enter your password:")
  14.             if temp2.isdigit():
  15.                 guess = int(temp2)
  16.             else:
  17.                 print('Please give an integer!')
  18.                 continue
  19.             if guess == 110110110110:
  20.                 print ("welcome")
  21.             else:
  22.                 print ("Sorry, the password is wrong. Call the 小甲鱼 to get the universal password")
  23.     else:
  24.         print("..who are you..Your invasion has informed the owner")
复制代码


第三种,有try except 语句,检测ValueError异常,具体如下:
  1. guess = 0
  2. while guess != 110110110110:
  3.     print("welcome BayMax Secret")
  4.     temp=input("Please input account number:")
  5.     try:
  6.         guess = int(temp)
  7.     except ValueError:
  8.         print('Please give an integer!')
  9.         continue
  10.     if guess == 1637000313:
  11.         print("Correct account number:1637000313")
  12.         while guess != 110110110110:
  13.             temp2=input("Please enter your password:")
  14.             try:
  15.                 guess = int(temp)
  16.             except ValueError:
  17.                 print('Please give an integer!')
  18.                 continue
  19.             if guess == 110110110110:
  20.                 print ("welcome")
  21.             else:
  22.                 print ("Sorry, the password is wrong. Call the 小甲鱼 to get the universal password")
  23.     else:
  24.         print("..who are you..Your invasion has informed the owner")
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2019-2-19 23:42:35 | 显示全部楼层
杯具菜鸟 发表于 2019-2-19 21:55
guess = 0
while guess != 110110110110:
    print("welcome BayMax Secret")
  1. try:
  2.     a = int(b)
  3. except:
  4.     print("输入的不是整数")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-2-20 00:30:43 | 显示全部楼层
哇这么晚了  谢谢老师们的解答 !
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-2-20 00:55:09 From FishC Mobile | 显示全部楼层
19304270021304 发表于 2019-2-19 22:44
我能想到三种方法
第一种,对于你来说应该最容易理解,直接把密码检测改成字符串,就不用int()转数字了 ...

老师您写的的代码走的程序还是原来那样嘛
guess=0和while guess !是什么意思呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-2-20 02:29:12 | 显示全部楼层
IamBayMax 发表于 2019-2-20 00:55
老师您写的的代码走的程序还是原来那样嘛
guess=0和while guess !是什么意思呢

guess = 0  #设置变量,不然下面不能判断
while guess != 110110110110:  #guess 不等于110110110110  
while需要假的条件才能退出循环
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-2-20 22:31:41 | 显示全部楼层    本楼为最佳答案   
IamBayMax 发表于 2019-2-20 00:55
老师您写的的代码走的程序还是原来那样嘛
guess=0和while guess !是什么意思呢

guess=0和while guess !是你原来的呀,我没有改;
你说的还是原来那样是不是指第一种,第一种确实不会提醒用户“Please give an integer!”,因为第一种只是把密码检测由数字改为字符串,用户输入非整数就不会报错,后面两种就可以满足你的需求了吧。
刚刚看了一下发现第三种有一个小错误,第15行guess = int(temp)应该改为guess = int(temp2),不好意思。
另外,你的代码思路应该是先让用户循环输入正确的用户名,然后再循环输入正确的密码,对吧?但你原来的写法(包括我昨晚的三段代码,没有改你的思路),如果用户在输入用户名阶段输入了110110110110,就会跳出循环。我觉得要实现你的想法,像下面这样可能会更好,不必在循环里套循环,直接分成两段,更明晰。
while True:
    print("welcome BayMax Secret")
    temp=input("Please input account number:")
    try:
        guess = int(temp)
    except ValueError:
        print('Please give an integer!')
        continue
    if guess == 1637000313:
        print("Correct account number:1637000313")
        break
    else:
        print("..who are you..Your invasion has informed the owner")

while True:
    temp2=input("Please enter your password:")
    try:
        guess = int(temp2)
    except ValueError:
        print('Please give an integer!')
        continue
    if guess == 110110110110:
        print ("welcome")
        break
    else:
        print ("Sorry, the password is wrong. Call 小甲鱼 to get the universal password")

By the way, 我不是老师,也是小白一枚~~
   

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-2-20 22:32:36 | 显示全部楼层
19304270021304 发表于 2019-2-20 22:31
guess=0和while guess !是你原来的呀,我没有改;
你说的还是原来那样是不是指第一种,第一种确实不会提 ...

忘了插成代码形式,见谅。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-2-20 22:50:32 | 显示全部楼层
确实是错误检测比较简单一些。 循环套循环确实不如两个循环简介明了。 感谢!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-4-2 02:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表