Python:跳过用户输入并稍后调用

时间:2018-05-06 11:39:11

标签: python

我对Python很陌生,目前我正在通过制作一些在工作中使用的脚本来学习。真正的简单,只需要用户输入并将其存储在稍后要调用的字符串中。问题是肯定/否答案,但我希望用户可以选择跳过,并在最后再次询问问题,我该怎么做?

目前这就是我所拥有的:

import sys
yes = ('yes', 'y')
no = ('no', 'n')
skip = ('skip', 's')
power = str(raw_input("Does the site have power? (Yes/No): "))
if power.lower() in yes:
  pass
elif power.lower() in no:
  pass
elif power.lower() in skip:
  pass
else:
  print ''
  print '%s is an invlaid input! Please answer with Yes or No' % power
  print ''
  exit()

然后在脚本结束后,在询问了所有问题之后我得到了这个:

if power.lower() in skip:
  power = str(raw_input("Does the site have power? (Yes/No): "))
  if power.lower() in yes:
    pass
  elif power.lower() in no:
    pass
  else:
    print ''
    print '%s is an invlaid input! Please answer with Yes or No' % power
    print ''
    exit()
else:
  pass

if power.lower == 'yes':
  print 'Site has power'
else:
  print 'Site doesnt have power, NFF.'

我知道这非常混乱,我只是在寻求指导/帮助。

此致 陷阱。

2 个答案:

答案 0 :(得分:2)

由于您不熟悉Python,我会给您一些提示:

  1. 存储所有收到的问题"跳过"作为对list的回复。
  2. 在所有问题结束时,迭代(提示:" for"循环)用户跳过的所有问题并再次询问。

答案 1 :(得分:0)

# Display the sum
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
message=("is this correct")
Print=(message)
#store input
answer = input("yes/no:")
if answer == "yes": print("thank you")
if answer == "no": print("hmm, are you sure?")
#store input
answer = input("yes/no:")
if answer == "yes" : print("please call my suppot hotline: +47 476 58 266")
if answer == "no" : print("ok goodbye:)")


if someone had a solution for line 7 when answerd yes skiped to the end desplaing ok `goodbye:) thank you`