Ini kode saya:

import random
name="srp"
age=14
gender="male"
vote="N/A"
def about_user():
    aboutusr1="Your name is",name,".\nYou are",age,"years old.\nYour gender is",gender,".\nFun fact -",vote," :)"
    aboutusr2="You are my master whose name is",name,".\nYour age is",age,"and your gender is",gender,".\nAlso, I know that",vote
    L1=[aboutusr1, aboutusr2]
    c=random.choice(L1)
    print(c)
abs=input("Ask me :")
if "about me" in abs:
    about_user()

Keluaran:

Tanya saya :tentang saya ('Nama Anda', 'srp', '.\nAnda', 14, 'tahun.\nJenis kelamin Anda adalah', 'laki-laki', '.\nFakta menyenangkan -', 'N/ SEBUAH', ' :)')

Tapi saya tidak ingin tanda kurung buka dan tutup, tanda kutip tunggal. Juga, hal-hal seperti \n untuk baris baru juga tidak dicetak. Tolong bantu aku!

0
SRP 27 Januari 2020, 15:56

2 jawaban

Jawaban Terbaik

Ubah aboutusr1 dan aboutusr2 Anda seperti di bawah ini (menggunakan str.format())

aboutusr1= "Your name is {}.\nYou are {} years old.\nYour gender is {}.\nFun fact - {} :)".format(name,age,gender,vote)
aboutusr2= "You are my master whose name is {}.\nYour age is {} and your gender is {}.\nAlso, I know that {}".format(name,age,gender,vote)

Keluaran

You are my master whose name is srp.
Your age is 14 and your gender is male.
Also, I know that N/A

Atau seperti yang dijelaskan oleh @sim di komentar gunakan print(*c) dalam kode Anda (* akan membongkar tuple)

0
Shijith 27 Januari 2020, 13:15

Jika Anda menggunakan python 3 Anda dapat menggunakan fStrings:

import random
name="srp"
age=14
gender="male"
vote="N/A"
def about_user(name,age,gender,vote):
    aboutusr1=f"Your name is {name},\nYou are {age},years old.\nYour gender is {gender}.\nFun fact - {vote} :)"
    aboutusr2=f"You are my master whose name is {name}.\nYour age is {age}, and your gender is {gender}.\nAlso, I know that {vote}"
    L1=" ".join([aboutusr1, aboutusr2])
    #Don't understand why this line
    #c=random.choice(L1)
    print(L1)


abs=input("Ask me :")
if "about me" in abs:
    about_user(name,age,gender,vote)
0
Renaud 27 Januari 2020, 13:15