簡體   English   中英

Python:跳過用戶輸入並稍后調用

[英]Python: Skip user input and call later on

我對 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.'

我知道這很混亂,我只是在尋找指導/幫助。

問候,陷阱。

由於您對 Python 比較陌生,我會給您一些提示:

  1. 將所有接收“跳過”作為響應的問題存儲到列表中
  2. 在所有問題結束時,迭代(提示:“for”循環)用戶跳過並再次詢問的所有問題。
# 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`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM