Python - 在if语句中找不到语法错误

时间:2016-06-11 19:14:46

标签: python if-statement syntax-error

自从我编写代码以来已经有一段时间了,所以我想再次回到它,但是我的一些代码遇到了问题。

我想编写一个简单的程序,它接受用户的输入并检查是否所有字母都没有空格,并且长度小于12.我每次在第17行都会出现“无效语法”错误运行代码,指向if语句之后的冒号,检查用户名是否只是字母且少于12个字符。我知道这意味着在此之前就行了一个错误,但在哪里?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="IP-address"
    android:textAllCaps="true" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Spinner
        android:id="@+id/spinner_ip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:entries="@array/spinner_ip_visual"
        android:entryValues="@array/spinner_ip_values" />

    <EditText
        android:id="@+id/edit_text_ip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="8" />

    <ImageButton
        android:id="@+id/image_button_save_ip"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@null"
        android:src="@drawable/ic_done_black_24dp"/>
</LinearLayout>


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="unit"
    android:textAllCaps="true" />

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/button_unit_celsius"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="Celsius"
        android:textAllCaps="true" />

    <RadioButton
        android:id="@+id/button_unit_fahrenheit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fahrenheit"
        android:textAllCaps="true" />
</RadioGroup>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="onButtonClickGenerateRandomData"
    android:text="Generate random data"
    android:textAllCaps="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="onButtonClickDropDatabase"
    android:text="delete weatherdata"
    android:textAllCaps="true" />

更新 - 我更多地使用了代码,我唯一改变的是#import the os module import os #Declare Message print "Welcome to Userspace - Your One-Stop Destination to Greatness!" + "\n" + "Please enter your username below." \ + "\n" + "\n" + "Username must be at least 12 characters long, with no spaces or symbols." + "\n" + "\n" #uinput stands for user's input uinput = raw_input("Enter a Username: ") #check_valid checks to see if arguement meets requirements def check_valid(usrnameinput): if (usrnameinput != usrnameinput.isalpha()) or (len(usrnameinput) >= 12): os.system('cls') print "Invalid Username" return False else: os.system('cls') print "Welcome, %s!" % (usrnameinput) return True #Asks for username and checks if its valid print uinput check_valid(uinput) #Checks input to check_valid is correct, and if it is not, then ask for new username input and checks it again while check_valid(uinput): return True break else: print uinput check_valid(uinput) print "We hope you enjoy your stay here at Userspace!" 条件while

if

我运行了此代码,但却收到了此错误:

print uinput
check_valid(uinput)

#Checks input to check_valid is correct, and if it is not, then ask for new username input and checks it again

if check_valid(uinput):
    print uinput
    check_valid(uinput)

print "We hope you enjoy your stay here at Userspace!"

很抱歉这样的菜鸟。今天刚加入Stack Overflow。

1 个答案:

答案 0 :(得分:0)

我相信这就是你想要的。我建议将它分成两个函数,check_valid()和一般的main()函数。

def check_valid(usrnameinput):

    if (not usrnameinput.isalpha()) or (len(usrnameinput) >= 12):
        print("Invalid name")
        return False
    else:
        print("Welcome!")
        return True

def main():

    uinput = raw_input("Enter a Username: ")
    while not check_valid(uinput): #Repeatedly asks user for name.
        uinput = raw_input("Enter a Username: ")

main()