标题:请教一下,如何在用int(input())函数时候输入字符串时候可以不出错
只看楼主
LanXX
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2019-8-5
结帖率:16.67%
已结贴  问题点数:10 回复次数:2 
请教一下,如何在用int(input())函数时候输入字符串时候可以不出错
谢谢各位大神,帮帮我,我需要的的确是整数,但怎么才可以输入了字符串也不会出错呢?


print('You enter a dark room with two doors.\nDo you go through oor #1 or door #2?')
door = int(input('>'))

if door == 1:
    print('There\'s a giant bear here eating a cheese cake.')
    print('what do you do?')
    print('1.Take the cake. \n2.Scream at the bear.')
    num = int(input('>'))

    for i in range(1):
        if num == 1:
            print('The bear eats your face off.\tGood job!')

        if num == 2:
            print('The bear eats your legs off.\tGood job!')

        else:
            print(f'{num} is not option, do it agains.')
            continue
else:
    print('you\'re dead')
搜索更多相关主题的帖子: 出错 input print int the 
2019-11-27 14:51
fall_bernana
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:17
帖 子:240
专家分:2086
注 册:2019-8-16
得分:10 
以下是引用LanXX在2019-11-27 14:51:15的发言:

谢谢各位大神,帮帮我,我需要的的确是整数,但怎么才可以输入了字符串也不会出错呢?


print('You enter a dark room with two doors.\nDo you go through oor #1 or door #2?')
door = int(input('>'))

if door == 1:
    print('There\'s a giant bear here eating a cheese cake.')
    print('what do you do?')
    print('1.Take the cake. \n2.Scream at the bear.')
    num = int(input('>'))

    for i in range(1):
        if num == 1:
            print('The bear eats your face off.\tGood job!')

        if num == 2:
            print('The bear eats your legs off.\tGood job!')

        else:
            print(f'{num} is not option, do it agains.')
            continue
else:
    print('you\'re dead')

程序代码:
print('You enter a dark room with two doors.\nDo you go through oor #1 or door #2?')
door = input('>')
while not door.isdigit():
    print("must int\n")
    door = input('>')

if int(door) == 1:
    print('There\'s a giant bear here eatinsg a cheese cake.')
    print('what do you do?')
    print('1.Take the cake. \n2.Scream at the bear.')
    num = input('>')
    while not num.isdigit():
        print("must int\n")
        num = input('>')

    for i in range(1):
        if int(num) == 1:
            print('The bear eats your face off.\tGood job!')

        elif int(num) == 2:
            print('The bear eats your legs off.\tGood job!')

        else:
            print(f'{num} is not option, do it agains.')
            continue
else:
    print('you\'re dead')
2019-11-27 15:59
LanXX
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2019-8-5
得分:0 
回复 2楼 fall_bernana
谢谢大神解答!!!
2019-11-29 14:16



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-498111-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.334613 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved