鱼C论坛

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

s.isdigit()使用问题

[复制链接]
发表于 2019-5-5 22:40:55 | 显示全部楼层 |阅读模式

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

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

x
新手自己看视频写的,请问:
1.在这基础上如何使用s.isdigit() 来提示输出类型错误并要求重新输入
2.整个过程是否有更简便的写法
谢谢
import random
times = 3
x = random.randint(1,10)
print("A Small Game")
temp=input("Please guess which integer in my mind: ")
guess = int(temp)
if guess == x:
  print("Woa, how do you know my mind!")
  print("but i have no gift for you~")
else :
  while (guess != x) and (times > 0):   
    if guess > x:
      print("what a pity, it's too big.")
    else:
      print("what a pity, it's too small.")
    temp = input("Please guess it again:")
    guess = int(temp)
    times = times - 1
    if times == 0:
      print("Sorry,you have no chance")
    if guess == x:
      print("Woa, how do you know my mind!")
      print("but i have no gift for you~")
print("Game Over.")
from time import sleep
sleep(15)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-5 23:11:53 | 显示全部楼层
现有代码,如果输入非数字肯定会报错。

  1. import random
  2. from time import sleep

  3. def getint(msg):
  4.     s=input(msg)            
  5.     while not s.isdigit():
  6.         s=input('请输入数字:')
  7.     return int(s)   

  8. times = 3
  9. x = random.randint(1,10)
  10. print("A Small Game")
  11. guess=getint("Please guess which integer in my mind: ")
  12. if guess == x:
  13.   print("Woa, how do you know my mind!")
  14.   print("but i have no gift for you~")
  15. else :
  16.   while (guess != x) and (times > 0):   
  17.     if guess > x:
  18.       print("what a pity, it's too big.")
  19.     else:
  20.       print("what a pity, it's too small.")
  21.     guess = getint("Please guess it again:")
  22.     times = times - 1
  23.     if times == 0:
  24.       print("Sorry,you have no chance")
  25.     if guess == x:
  26.       print("Woa, how do you know my mind!")
  27.       print("but i have no gift for you~")
  28. print("Game Over.")

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

使用道具 举报

发表于 2019-5-5 23:14:09 | 显示全部楼层
  1. import random
  2. from time import sleep

  3. times = 3
  4. x = random.randint(1,10)
  5. print("A Small Game")
  6. while times :
  7.     while True :
  8.         temp = input("Please guess which integer in my mind: ")
  9.             if temp.isdigit():
  10.                 break
  11.             else:
  12.                print("Input an integer please.")
  13.     guess = int(temp)
  14.     if guess == x:
  15.         print("Woa, how do you know my mind!")
  16.         print("but i have no gift for you~")
  17.         times = 0
  18.     else :
  19.         times -= 1
  20.         if times:
  21.             if guess > x:
  22.                 print("what a pity, it's too big.")
  23.             else:
  24.                 print("what a pity, it's too small.")
  25.             print("Please guess it again:")
  26.         else:
  27.             print("Sorry, you have no chance")
  28. print("Game Over.")
  29. sleep(15)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-7 21:01:40 | 显示全部楼层
ba21 发表于 2019-5-5 23:11
现有代码,如果输入非数字肯定会报错。

谢谢你的回复
我才看到入门的第五课
下面的这部分我还没学到
def getint(msg):
    s=input(msg)            
    while not s.isdigit():
        s=input('请输入数字:')
    return int(s)   
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-7 21:04:02 | 显示全部楼层

我运行后第十行报错显示
indentationerror:unexpected indent
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-15 15:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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