鱼C论坛

 找回密码
 立即注册
查看: 2488|回复: 4

[已解决]第五课课后习题动手题

[复制链接]
发表于 2020-3-7 21:54:49 | 显示全部楼层 |阅读模式

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

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

x
改进小游戏,提醒用户输入错误格式
  1. import random
  2. times = 3
  3. secret = random.randint(1,10)
  4. print('Welcome!!!!!!')
  5. guess = 0
  6. print("Guess what number do I think?", end = " ")
  7. while (guess != secret) and (times > 0):
  8.     guess = int(input())
  9.     while type(guess) != type(1):#应该是这里出问题了
  10.         print("Illegal type!")
  11.     times = times - 1
  12.     if guess == secret:
  13.         print("Bingo!")
  14.         print("Huh, without prize!")
  15.     else:
  16.         if guess > secret:
  17.             print("Too big!")
  18.         else:
  19.             print("Too small!")
  20.         if times > 0:
  21.             print("Try again!")
  22.         else:
  23.             print("You are out of chance!")
  24. print("Game over!")
复制代码
最佳答案
2020-3-7 22:12:19
Easier 发表于 2020-3-7 22:11
我知道了,但是如果这样写 guess的赋值就改变了,再重新输入其他数就会报错

这样试试:

  1. import random

  2. times = 3
  3. secret = random.randint(1, 10)
  4. print('Welcome!!!!!!')
  5. guess = 0
  6. print("Guess what number do I think?", end=" ")
  7. while (guess != secret) and (times > 0):
  8.     guess = input()
  9.     while not guess.isdigit():
  10.         guess = input("Illegal type! Please input: ")
  11.     guess = int(guess)
  12.     times = times - 1
  13.     if guess == secret:
  14.         print("Bingo!")
  15.         print("Huh, without prize!")
  16.     else:
  17.         if guess > secret:
  18.             print("Too big!")
  19.         else:
  20.             print("Too small!")
  21.         if times > 0:
  22.             print("Try again!")
  23.         else:
  24.             print("You are out of chance!")
  25. print("Game over!")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-7 21:57:53 | 显示全部楼层
你的思路是错的,guess = int(input()) 这一句已经将 guess 转化为 int() 了,如果用户不输入整数会直接报错。

而且判断一个字符串可不可以强制转化为整数,需要使用字符串的 isdigit() 方法。

代码帮你改了:

  1. import random

  2. times = 3
  3. secret = random.randint(1, 10)
  4. print('Welcome!!!!!!')
  5. guess = 0
  6. print("Guess what number do I think?", end=" ")
  7. while (guess != secret) and (times > 0):
  8.     guess = input()
  9.     while not guess.isdigit():
  10.         guess = input("Illegal type! Please input: ")
  11.     times = times - 1
  12.     if guess == secret:
  13.         print("Bingo!")
  14.         print("Huh, without prize!")
  15.     else:
  16.         if guess > secret:
  17.             print("Too big!")
  18.         else:
  19.             print("Too small!")
  20.         if times > 0:
  21.             print("Try again!")
  22.         else:
  23.             print("You are out of chance!")
  24. print("Game over!")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-7 22:11:25 | 显示全部楼层
zltzlt 发表于 2020-3-7 21:57
你的思路是错的,guess = int(input()) 这一句已经将 guess 转化为 int() 了,如果用户不输入整数会直接报 ...

我知道了,但是如果这样写
  1. guess = input("Illegal type! Please input: ")
复制代码
guess的赋值就改变了,再重新输入其他数就会报错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-7 22:12:19 | 显示全部楼层    本楼为最佳答案   
Easier 发表于 2020-3-7 22:11
我知道了,但是如果这样写 guess的赋值就改变了,再重新输入其他数就会报错

这样试试:

  1. import random

  2. times = 3
  3. secret = random.randint(1, 10)
  4. print('Welcome!!!!!!')
  5. guess = 0
  6. print("Guess what number do I think?", end=" ")
  7. while (guess != secret) and (times > 0):
  8.     guess = input()
  9.     while not guess.isdigit():
  10.         guess = input("Illegal type! Please input: ")
  11.     guess = int(guess)
  12.     times = times - 1
  13.     if guess == secret:
  14.         print("Bingo!")
  15.         print("Huh, without prize!")
  16.     else:
  17.         if guess > secret:
  18.             print("Too big!")
  19.         else:
  20.             print("Too small!")
  21.         if times > 0:
  22.             print("Try again!")
  23.         else:
  24.             print("You are out of chance!")
  25. print("Game over!")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-7 22:14:43 | 显示全部楼层

懂了,谢谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-27 13:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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