2016-04-02 63 views
-1

當您運行程序並選擇某些內容時,它有時不會確認輸入。對不起,如果有一個明顯的錯誤。我在這方面還很新。感謝您提前提供的所有幫助。 這不是一個大問題,但它是一個學校的事情,所以最好我想解決它。請求輸入,但不確認

import webbrowser as web 
import random as rand 
print('1: Website Listings') 
print('2: Password Generator') 
print('3: Number Guessing Game') 
print('4: Calculator') 
while True: 
    user_input1 = str(input(': ')) 
    print('') 

#Website Listings: 

    if user_input1 == '1': 
     print('1: Google') 
     print('2: Youtube') 
     print('3: 9GAG') 
     print('4: CanYouRunIt') 

     user_input2 = str(input(': ')) 

     if user_input2 == '1': 
      web.open('https://www.google.co.za/') 
      print('') 
      print('1: Website Listings') 
      print('2: Password Generator') 
      print('3: Number Guessing Game') 
      print('4: Calculator') 
      user_input1 = str(input(': ')) 

     if user_input2 == '2': 
      web.open('https://www.youtube.com/?gl=ZA') 
      print('') 
      print('1: Website Listings') 
      print('2: Password Generator') 
      print('3: Number Guessing Game') 
      print('4: Calculator') 
      user_input1 = str(input(': ')) 

     if user_input2 == '3': 
      web.open('http://9gag.com/') 
      print('') 
      print('1: Website Listings') 
      print('2: Password Generator') 
      print('3: Number Guessing Game') 
      print('4: Calculator') 
      user_input1 = str(input(': ')) 


     if user_input2 == '4': 
      web.open('http://www.systemrequirementslab.com/cyri') 
      print('') 
      print('1: Website Listings') 
      print('2: Password Generator') 
      print('3: Number Guessing Game') 
      print('4: Calculator') 
      user_input1 = str(input(': ')) 

#Password Generator: 

    if user_input1 == '2': 
     total_digits = 0 
     max_digits = int(input('How many digits? ')) 
     while total_digits != max_digits: 
      print(rand.randint(0,9)) 
      total_digits+=1 
     print('') 
     print('1: Website Listings') 
     print('2: Password Generator') 
     print('3: Number Guessing Game') 
     print('4: Calculator') 
     user_input1 = str(input(': ')) 

#Number Guessing Game: 
    if user_input1 == '3': 
     print('') 
     lower_value = int(input('Lowest Number: ')) 
     print(' ') 
     higher_value = int(input('Highest Number: ')) 
     print(' ') 
     tries = int(input('Amount of tries before failure: ')) 
     print(' ') 
     answer = rand.randint(lower_value, higher_value) 

     while tries != 0: 
      guess = int(input('Your guess: ')) 
      if guess > answer: 
       print('The answer is smaller') 
       tries-=1 
       print('You have ' + str(tries) + ' try(ies)' + ' left') 
       print(' ') 
      elif guess < answer: 
       print('The answer is larger') 
       tries-=1 
       print('You have ' + str(tries) + ' try3(ies)' + ' left') 
       print(' ') 
      elif guess == answer: 
       print('!!! YOU WON !!!') 
       print('You had ' + str(tries) + ' try(ies)' + ' left') 
       print(' ') 
       break 
      if tries == 0: 
       print('!!! YOU LOSE !!!') 
       print(' ') 
       break 
     print('') 
     print('1: Website Listings') 
     print('2: Password Generator') 
     print('3: Number Guessing Game') 
     print('4: Calculator') 
     user_input1 = str(input(': ')) 

#Calculator: 

    print('')  
    if user_input1 == '4': 
     print('These are your options: ') 
     print('Type "1" to add two numbers') 
     print('Type "2" to subtract two numbers') 
     print('Type "3" to multiply two numbers') 
     print('Type "4" to divide two numbers') 
     print('Type "5" to determine the product of an exponent') 
     print('Type "6" to return to main screen') 
     print('') 

     user_input3 = input('What do you want to do? ') 

     if user_input3 == '6': 
      print('') 
      print('1: Website Listings') 
      print('2: Password Generator') 
      print('3: Number Guessing Game') 
      print('4: Calculator') 
      user_input1 = str(input(': ')) 

     elif user_input3 == '1': 
      num1 = float(input('Please enter the first number: ')) 
      num2 = float(input('Please enter a second number to add: ')) 
      result=str(num1 + num2) 
      print('The answer is: ' + result) 
      print('') 
      print('1: Website Listings') 
      print('2: Password Generator') 
      print('3: Number Guessing Game') 
      print('4: Calculator') 
      user_input1 = str(input(': ')) 

     elif user_input3 == '2': 
      num1 = float(input('Please enter the first number: ')) 
      num2 = float(input('Please enter a second number to subtract: ')) 
      result=str(num1 - num2) 
      print('The answer is: ' + result) 
      print('') 
      print('1: Website Listings') 
      print('2: Password Generator') 
      print('3: Number Guessing Game') 
      print('4: Calculator') 
      user_input1 = str(input(': ')) 

     elif user_input3 == '3': 
      num1 = float(input('Please enter the first number: ')) 
      num2 = float(input(' Please enter a second number to multiply: ')) 
      result = str(num1 * num2) 
      print('The answer is: ' + result) 
      print('') 
      print('1: Website Listings') 
      print('2: Password Generator') 
      print('3: Number Guessing Game') 
      print('4: Calculator') 
      user_input1 = str(input(': ')) 

     elif user_input3 == '4': 
      num1 = float(input('Please enter the first number ')) 
      num2 = float(input('Please enter a second number to divide by: ')) 
      result = str(num1/num2) 
      print('The answer is: ' + result) 
      print('') 
      print('1: Website Listings') 
      print('2: Password Generator') 
      print('3: Number Guessing Game') 
      print('4: Calculator') 
      user_input1 = str(input(': ')) 

     elif user_input3 == '5': 
      num1 = float(input('Please enter the base number: ')) 
      num2 = float(input('Please enter the exponent: ')) 
      result = str(num1 ** num2) 
      print('The answer is: ' + result) 
      print('') 
      print('1: Website Listings') 
      print('2: Password Generator') 
      print('3: Number Guessing Game') 
      print('4: Calculator') 
      user_input1 = str(input(': ')) 

     else: 
      print('Unknown command') 
      print('') 
      print('1: Website Listings') 
      print('2: Password Generator') 
      print('3: Number Guessing Game') 
      print('4: Calculator') 
      user_input1 = str(input(': ')) 

回答

-1

input已經返回一個字符串,所以在再次強制轉換爲字符串使用str沒有意義的。因爲你的投入將是一個數字,將其轉換爲整數,而不是

# ... rest of your code 

inp = input("Enter a numerical option: ") 
try: 
    inp = int(inp) 
except: 
    print(inp, "is not a valid option") 
    continue # this will skip everything below and go to the next instance of the while loop which will ask the user again for input 

# ... rest of your code 

在問候代碼的結構,它可以分裂成更小的功能,可以代替被調用。

def webListings(): 
    print('1: Google') 
    ... 

def passGen() 
    print(...) 
    ... 

然後你就可以做這樣的事情

if user_input1 == 1: 
    webListings() 
else if user_input1 == 2: 
    passGen() 
0

在所有第一結束if語句,添加了一通。