我正在努力学习Python艰苦的练习35.以下是原始代码,我们要求对其进行更改,以便它可以接受不具有0和1的数字它们。
def gold_room():
print "This room is full of gold. How much do you take?"
next = raw_input("> ")
if "0" in next or "1" in next:
how_much = int(next)
else:
dead("Man, learn to type a number.")
if how_much < 50:
print "Nice, you're not greedy, you win!"
exit(0)
else:
dead("You greedy bastard!")
这是我的解决方案,运行正常并识别浮点值:
def gold_room():
print "This room is full of gold. What percent of it do you take?"
next = raw_input("> ")
try:
how_much = float(next)
except ValueError:
print "Man, learn to type a number."
gold_room()
if how_much <= 50:
print "Nice, you're not greedy, you win!"
exit(0)
else:
dead("You greedy bastard!")
通过搜索类似的问题,我找到了一些帮助我编写另一个解决方案的答案,如下面的代码所示。问题是,使用isdigit()不会让用户输入浮点值。因此,如果用户表示他们想要获得50.5%,那么它会告诉他们学习如何键入数字。它适用于整数。我怎么能绕过这个?
def gold_room():
print "This room is full of gold. What percent of it do you take?"
next = raw_input("> ")
while True:
if next.isdigit():
how_much = float(next)
if how_much <= 50:
print "Nice, you're not greedy, you win!"
exit(0)
else:
dead("You greedy bastard!")
else:
print "Man, learn to type a number."
gold_room()
答案 0 :(得分:2)
isinstance(next, (float, int))
已经从字符串转换,那么 next
就可以解决这个问题。它不是在这种情况下。因此,如果您想避免使用re
,则必须使用try..except
进行转换。
我建议您使用之前的try..except
块而不是if..else
块,但将更多代码放入其中,如下所示。
def gold_room():
while True:
print "This room is full of gold. What percent of it do you take?"
try:
how_much = float(raw_input("> "))
if how_much <= 50:
print "Nice, you're not greedy, you win!"
exit(0)
else:
dead("You greedy bastard!")
except ValueError:
print "Man, learn to type a number."
这将尝试将其强制转换为浮动,如果失败则会引发将被捕获的ValueError
。要了解详情,请参阅其上的Python Tutorial。
答案 1 :(得分:1)
RE将是一个不错的选择
>>> re.match("^\d+.\d+$","10")
>>> re.match("^\d+.\d+$","1.00001")
<_sre.SRE_Match object at 0x0000000002C56370>
如果原始输入是一个浮点数,它将返回一个对象。否则,它将返回None。如果你需要识别int,你可以:
>>> re.match("^[1-9]\d*$","10")
<_sre.SRE_Match object at 0x0000000002C56308>
答案 2 :(得分:1)
答案 3 :(得分:1)
我的方法存在的问题是,你走的是“先跳过去”而不是更多Pythonic“更容易请求宽恕而不是权限”的道路。我认为您的原始解决方案比尝试以这种方式验证输入更好。
以下是我的写作方式。
GREEDY_LIMIT = 50
def gold_room():
print("This room is full of gold. What percent of it do you take?")
try:
how_much = float(raw_input("> "))
except ValueError:
print("Man, learn to type a number.")
gold_room()
return
if how_much <= GREEDY_LIMIT:
print "Nice, you're not greedy, you win!"
exit(0)
else:
dead("You greedy bastard!")
答案 4 :(得分:0)
使用下面基于python的正则表达式检查浮点字符串
import re
a=re.match('((\d+[\.]\d*$)|(\.)\d+$)' , '2.3')
a=re.match('((\d+[\.]\d*$)|(\.)\d+$)' , '2.')
a=re.match('((\d+[\.]\d*$)|(\.)\d+$)' , '.3')
a=re.match('((\d+[\.]\d*$)|(\.)\d+$)' , '2.3sd')
a=re.match('((\d+[\.]\d*$)|(\.)\d+$)' , '2.3')
输出:!无,!无,!无,无,!无 然后使用此输出进行转换。
答案 5 :(得分:0)
如果您不想使用try / except,以下是我的回答:
def gold_room():
print "This room is full of gold. How much do you take?"
choice = input("> ")
if choice.isdigit():
how_much = int(choice)
elif "." in choice:
choice_dot = choice
choice_dot_remove = choice_dot.replace(".","")
if choice_dot_remove.isdigit():
how_much = float(choice)
else:
dead("Man, learn to type a number.")
if how_much < 50:
print "Nice, you're not greedy, you win!"
exit(0)
else:
dead("You greedy bastard!")