2016-04-02 15 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

ような何かを、パスを追加することができます。

関連する問題