小编Ker*_*tto的帖子

如何在Python中比较字符串和整数?

我是Python的新手。我写了这个,当我在输入中输入字母时出现了这个错误:

TypeError: unorderable types: str() >= int()
Run Code Online (Sandbox Code Playgroud)

这是我编写的代码:

user_input = input('How old are you?: ')
if user_input >= 18:
   print('You are an adult')
elif user_input < 18:
     print('You are quite young')
elif user_input == str():
     print ('That is not a number')
Run Code Online (Sandbox Code Playgroud)

python string int input

3
推荐指数
1
解决办法
6894
查看次数

随机调用要执行的函数

我做了这个简短的问卷调查:

from random import randint

def pancakes():
    q = raw_input("Do you like pancakes?")
    if q == "yes":
        print("Great!")
    elif q == "no":
        print("Hmmm...")

def french_toast():
    q = raw_input("Do you like french toast?")
    if q == "yes":
        print("Oh!")
    elif q == "no":
        print("Same here!")

def random():
    num = 2
    while num > 0:
        random = randint(1, 2)
        if random == 1:
            num = num -1
            pancakes()
         elif random == 2:
             num = num -1
             french_toast()

random()
Run Code Online (Sandbox Code Playgroud)

我的目标是以随机顺序提出问题.但有时同样的问题会被问到两次,因为它是随机选择的.

那么我怎么能让它只问一次相同的问题呢?

python random python-2.x

0
推荐指数
1
解决办法
119
查看次数

标签 统计

python ×2

input ×1

int ×1

python-2.x ×1

random ×1

string ×1